Josir
Josir

Reputation: 1644

Filter Queryset with null condition

I want to filter records enabled today:

today = datetime.today().date()
dataset = Structure.objects.exclude(final_date__lt=today).filter(initial_date__lte=today)

The above code works fine with both initial and final day are filled.

How can I construct the filter considering that final_date can be null ?

Or Is there a better construction for this query?

Upvotes: 0

Views: 114

Answers (1)

inigomedina
inigomedina

Reputation: 1831

If I understand this right, you would like to filter final_date for both value and null. Is it?

You could use Q objects for filtering.

from django.db.models import Q

dataset = Structure.objects.filter(Q(final_date__lt=today) | Q(final_date__isnull=True)).filter(initial_date__lte=today)

Upvotes: 2

Related Questions