Filter queryset by optional start and end date

615 Views Asked by At

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?

1

There are 1 best solutions below

0
On

I have a generic solution for this kind of problems. Reuse this custom queryset for all models

class MyQuerySet(models.QuerySet):

    def filter_if(self, **kwargs):
        new_kwargs = {a: b for (a, b) in kwargs.items() if b}
        return self.filter(new_kwargs)

results.filter_if(date_modified__gte=dt_from, date_modified__lte=dt_until)