Question

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.Model):
    start = models.DateField()
    title = models.CharField(_'title'), max_length=256)

myapp/filters.py

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 filter reference or Using django-filter guide. 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-filter

Was it helpful?

Solution

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>).

Solution:

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
    )

    ...

OTHER TIPS

You can filter datetime field by date

start = django_filters.DateFilter('start__date')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top