How To Use Django_filters.datefilter?
I'm trying to use django_filter's DateFilter to filter by an exact date, but could not get it to return any result. myapp/models.py from django.db import models class Event(models
Solution 1:
Found the issue.
The 'start'
field is a DateTimeField. So when querying only for the date, no match is found because it never matches the time.
For example:
If I enter 01/14/2012, it looks for start date datetime.date(2012, 01, 14, 0, 0)
, but the actual start date may be datetime.datetime(2012, 01, 14, 21, 0, tzinfo=<UTC>)
.
Answer :
Use lookup_type='startswith'
or 'lookup_type='contains'
(source) but 'contains'
appears to be faster
Class EventFilter(django_filters.FilterSet):
start = django_filters.DateFilter(
'start', label=_('With start date'),
lookup_type='contains'# use contains
)
...
Solution 2:
You can filter datetime field by date
start = django_filters.DateFilter('start__date')
Post a Comment for "How To Use Django_filters.datefilter?"