Question

I need to filter objects of model class. I want to query all objects where 'date' field is in some range and one of other 2 fields are True (one or both).

Following code doesn't appear tobe working:

visits = Visit.objects.filter(date__range=(now, delta), Q(sms_reminder=True) | Q(email_reminder=True))

any suggestions?

Était-ce utile?

La solution

Swap the order of your filters:

visits = Visit.objects.filter(
    Q(sms_reminder=True) | Q(email_reminder=True),
    date__range=(now, delta),
)

From the Q docs:

However, if a Q object is provided, it must precede the definition of any keyword arguments.

Standard python: args then kwargs. (Caught me out a few times)

Autres conseils

If your filter is (A OR B) AND C

You can try with this filter :

visits = Visit.objects.filter( (Q(sms_reminder=True)|Q(email_reminder=True)) & Q(date__range=(now, delta)))
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top