henriquebastos / django-aggregate-if

Conditional aggregates for Django queries, just like the famous SumIf and CountIf in Excel.
MIT License
138 stars 17 forks source link

Allow only clause to refer to fields not already joined to #5

Closed martsberger closed 10 years ago

martsberger commented 10 years ago

It should be possible to do a query like this:

publishers = Publisher.objects.annotate(jeff_books=Count('book', only=Q(book__contact__name__icontains='Jeff')))

This generates an error because the author table is not joined to. In this pull request, the only clause explicitly generates the necessary joins so that the above query will work.

Test included.

henriquebastos commented 10 years ago

That's pretty awesome! Thank you! I'll make a new release soon with your patch.