I'm trying to use django_filter's DateFilter to filter by an exact date, but could not get it to return any result.
from django.db import models class Event(models.Model): start = models.DateField() title = models.CharField(_'title'), max_length=256)
from myapp.models import Event import django_filters class EventFilter(django_filters.FilterSet): start = django_filters.DateFilter( 'start', label=_('With start date'), widget=forms.DateInput() # I'm using a datepicker-like widget to enter date ) class Meta: model = Event fields = ('start',)
For example: I have an event with start date 01/14/2012. When I entered that value in the date filter, it returns nothing.
I wasn't able to find a more in-depth description about DateFilter in django_filter's documentation site, either in its <a href="https://django-filter.readthedocs.org/en/latest/ref/filters.html" rel="nofollow">filter reference</a> or <a href="https://django-filter.readthedocs.org/en/latest/usage.html" rel="nofollow">Using django-filter guide</a>. Or anywhere in general. Any ideas what I might be doing wrong?
I don't expect there's anything wrong with the date format from the datepicker widget since it's being used in another form (to enter data) and it works fine.
Additional info: I'm using django-1.6 and whatever the last version of django-filterAnswer1:
Found the issue.
'start' field is a DateTimeField. So when querying only for the date, no match is found because it never matches the <em>time</em>.
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>).
'lookup_type='contains' (<a href="https://stackoverflow.com/a/11174559" rel="nofollow">source</a>) 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 ) ...