martsberger / django-pivot

A module for pivoting Django Querysets
MIT License
210 stars 16 forks source link

Pivot on Union Queryset : Calling QuerySet.distinct() after union() is not supported. #25

Open Bbillyben opened 1 year ago

Bbillyben commented 1 year ago

Hi there,

I'm trying to get some dashboarded datas on some models, and even between models.

issue description : When I union 2 queryset of models objects, and then try to pivot with pivot_table, I've got the issue :

django.db.utils.NotSupportedError: Calling QuerySet.distinct() after union() is not supported.

Step to reproduce :

  1. define a model
  2. add some new instances
  3. : a=models.MYModel.objects.filter(pk=1) b=models.MYModel.objects.filter(pk=2) c=a.union(b) pivot_table = pivot(c, 'field1', 'field2', 'field3')

=> raise the error above

is there anything I missed ?

infos : django v4.1.2 python v3.9.2 django-pivot v1.9.0

martsberger commented 1 year ago

I looked into this a bit. There are a few things Django doesn't let you do with a union queryset, as you note above, you can't call distinct() on it. You also can't call annotate(), which means the core of how the pivot function works would have to be re-written and I'm not sure it's even possible (depending on what kind of aggregate you want to do) to do that through the ORM.