Django ORM - query help
class Log(models.Model):
thing = models.ForeignKey(Thing)
context = models.CharField(max_length=255)
action = models.CharField(max_length=255)
modifier = models.ForeignKey(User, limit_choices_to={'groups__in':
[2]})
modified_on = models.DateTimeField(auto_now=True)
class Thing(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)
Hello,
Given the above models, how can I achieve the following results:
A unique "Thing" instance along with the last (in terms of date)
modified_on instance (ideally with access to the other related fields
such as modifier) from the "Log" model? I have tried using .annotate
but it seems to result in a very ugly left outer join which makes me
think I must be doing something wrong.
Such as:
last_modified =
Thing.objects.select_related().annotate(deletion_date=Max('log__modified_on')).order_by('-
deletion_date')
Any assistance would be much appreciated.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home