Pregunta

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?

¿Fue útil?

Solución

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)

Otros consejos

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)))
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top