when I combine two Q objects with | operator it ends up with AND being used in sql query.
|Reported by:||muaddib@…||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||1.2|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
Hello. I created two querysets which works fine.
Invoice.objects.annotate(mysum=Sum('payments__payment')).filter(Q(mysum__lte=F('invoice_total')) ) Invoice.objects.annotate(mysum=Sum('payments__payment')).filter(mysum=None)
queryset1 returns objects with mysum less then invoice_total. mysum sums payments for the particular invoice.
queryset2 catches non-numeric objects with 'None' as mysum value. This happens when there no payments for the particular invoice.
invoice_total is a decimal field.
payment is a related_set relation which holds a decimal, too.
Now to combine those two querysets into one i use Q logic.
Invoice.objects.annotate(mysum=Sum('payments__payment')).filter(Q(mysum=None) | Q(mysum__lte=F('invoice_total')) )
Unfortunatly it doesnt work (returned queryset is empty). In sql log I can see that those two conditions above are always being glued with default AND (should be ORed because I use Q | Q, not Q & Q).
Database is a postresql 8.3.
Change History (3)
comment:1 Changed 3 years ago by Alex
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
- Resolution set to worksforme
- Status changed from new to closed
comment:2 follow-up: ↓ 3 Changed 3 years ago by alejandroalonsofernandez@…
- Cc alejandroalonsofernandez@… added