I want to filter a queryset by a date range where both the start and end date are optional. Specifically,
if dt_from:
results = results.filter(date_modified__gte=dt_from)
if dt_until:
results = results.filter(date_modified__lte=dt_until)
where dt_from
and dt_until
are each either datetime.datetime
, datetime.date
, or None
. The documentation about the behaviour of chaining multiple filters is extremely confusing however (see Chaining multiple filter() in Django, is this a bug?), and I'm not sure that the above does what I think it does (it may OR the filters rather than ANDing them).
Does the above code achieve what I want (i.e. AND the two filters) or is there another way I should do this?