Tuesday, August 30, 2016

Re: A tricky query in one to many relationship - atleast for me:)

Hi Erik,

Thanks for the solution.

So, I understand we get all instances of B which are max (date created)  or latest (date created) for each instance of A and then we check b.text == 'ABCD' and select the corresponding instance of A. 
Can't we add additional filter to check if text='ABCD' instead of running it through the for loop? I am still yet to catch up with all the concepts of Django ORM hence, not sure about it.

Also, if we use Max('a__b__date_created') , do we need the following condition still in the for loop? :
    if b.date_created != b.date_created__max: 


On Tuesday, August 30, 2016 at 3:03:44 PM UTC+5:30, Erik Cederstrand wrote:
> for b in B.objects.all().select_related('a').annotate(Max('date_created')):

That should probably be: Max('a__b__date_created') instead.


You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/49ab42be-053d-442d-a9b9-51d5123529fc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment