Opened 14 years ago

Last modified 14 years ago

#15239 closed

when I combine two Q objects with | operator it ends up with AND being used in sql query. — at Initial Version

Reported by: muaddib@… Owned by: nobody
Component: Database layer (models, ORM) Version: 1.2
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hello. I created two querysets which works fine.

Invoice.objects.annotate(mysum=Sum('paymentspayment')).filter(Q(mysumlte=F('invoice_total')) )
Invoice.objects.annotate(mysum=Sum('paymentspayment')).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.

Fields used:
invoice_total is a decimal field.
paymentspayment is also decimal (in related_set).

Now to combine those two querysets into one i use Q logic.

Invoice.objects.annotate(mysum=Sum('paymentspayment')).filter(Q(mysum=None) | Q(mysumlte=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 (0)

Note: See TracTickets for help on using tickets.
Back to Top