Question regarding Generic Foreign Keys and relations

20 views
Skip to first unread message

David

unread,
May 8, 2012, 11:25:45 AM5/8/12
to django...@googlegroups.com
I am trying to abstract a log app that I have made for my project to make it more DRY. In order to achieve this I have tried to implement Generic FKs

class Modification(models.Model):
    content_type = models.ForeignKey(ContentType)
    object_id  = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id')
    action = models.PositiveSmallIntegerField(choices=ACTION_TYPE_CHOICES, db_index=True)
    modifier = models.ForeignKey(User)
    modified_on = models.DateTimeField(auto_now=True)

This appears to function as intended.

I also have another app that I would like to query the logs for:

class NewApp(models.Model):
    prefix = models.CharField(max_length=50,choices=PREFIXES, blank=True)
    first_name = models.CharField(max_length=255)
    last_name = models.CharField(max_length=255)
    modifications = generic.GenericRelation(Modification)

Is it possible please to query and return the relevant results from both models in one queryset? For example, I want to do something similar to this:

content_type = ContentType.objects.get_for_model(NewApp)
last_modified = Modification.objects.select_related().distinct('object_id').filter(content_type=content_type).order_by('object_id', '-modified_on')

The above, doesn't, but I need to achieve the fields from NewApp as well as Modification.

Thank you for any assistance.

David

unread,
May 10, 2012, 12:03:47 PM5/10/12
to django...@googlegroups.com
Just an update, the following got me on the right track.  http://blog.roseman.org.uk/2010/02/22/django-patterns-part-4-forwards-generic-relations/
Reply all
Reply to author
Forward
0 new messages