Home > other >  Date filter in django
Date filter in django

Time:11-06

Filter dates that have not passed

class Distributor(models.Model):
    expire_at = models.DateTimeField()

I want to get the data that has not expired

CodePudding user response:

from datetime import datetime


desired_query = Distributor.objects.filter(expire_at__gt=datetime.now())

The mentioned query in the above code snippet will return Distributor objects have not expired yet.

CodePudding user response:

Here the query would look like if you want the data where expire_at is null

Distributor.objects.filter(expire_at__isnull=True)

isnull() is a method of the QuerySet class and it returns True if the value of the field is null.

  • Related