Using Django 4.1.2, filtering does not work for database fields.
Given the following model:
class Activities(models.Model):
es_date = models.DateField(blank=True, null=True)
ef_date = models.DateField(blank=True, null=True)
ls_date = models.DateField(blank=True, null=True)
lf_date = models.DateField(blank=True, null=True)
Migration done and DB content can be retrieved, for instance it gives back all of them properly:
>>>from mymodel.models import Activities
>>>Activities.objects.all()
<QuerySet [<Activities: Task 33>, <Activities: Task 30>...]>
or requesting a particular item also works properly:
>>>Activities.objects.get(id=1)
<Activities: Task 1>
although applying filter for a given field it drops "NameError" error
>>>Activities.objects.all().filter(es_date>timezone.now())
Traceback (most recent call last):
File "<console>", line 1, in <module>
NameError: name 'es_date' is not defined
What might be the error?
CodePudding user response:
try this: Activities.objects.all().filter(es_date__gt=timezone.now())
and check the documentation: https://docs.djangoproject.com/en/4.1/ref/models/querysets/#gt
Regards
CodePudding user response:
You need to use __gt:
Activities.objects.all().filter(es_date__gt=timezone.now())