vendredi 31 juillet 2015

Replicating Count() on two querysets from different models in Django

I'm trying to query two models in Django. Both have similar data. Once the queries are merged I want to be able to count the occurences of unique values and add a count column to show this. So far I have:

queryCount = table.objects.values(last_name, first_name)
queryCount = queryCount.annotate(count=Count(last_name))

This works but only for one table. If I add another table (repeat the query with another table) I have options of how to merge the two queries.

I've tried itertools chain but that means I can no longer use distinct() to get unique values.

I've tried the snippet here but again no distinct() function.

I've also tried removing the .annotate(Count) and just getting all the values (without counting) and adding the count() method after merging the two querysets. I'm having the same problem as the merged querysets don't have the Count() method available.

Is this possible in Django or should I just drop to Raw SQL?

Aucun commentaire:

Enregistrer un commentaire