4612

How to use django_filters.DateFilter?

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 <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-filter

Answer1:

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 <em>time</em>.

<strong>For example:</strong>

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

<strong>Solution:</strong>

Use lookup_type='startswith' or '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 ) ...

Recommend

  • django-filter with django autocomplete-light
  • regular expression multiple matches
  • Choices defined for model fields not being enforced?
  • Django error 'unicode' object has no attribute 'objects'
  • NoReverseMatch at / Reverse error with urlresolvers, get_absolute_url()
  • Move an object (element) one step up with Javascript
  • Zend Framework 2 - Building a simple form with Validators
  • Replicating and differentiating portions of a form
  • Sum and Average of a series of numbers inputed to a text field
  • Thrust filter by key value
  • Spring MVC redirect with custom http headers
  • Django: ORDER BY DESC on FloatField puts null values top?
  • p:fileDownload in p:dataTable does not work (just refreshes page) after performing search on the p:d
  • How to change placeholder text in an autocomplete activity of android google place?
  • Blackberry 6: how to detect a long click on track pad?
  • ApplePay PKPaymentAuthorizationViewController always shows processing
  • Two Tables Serving as one Model in Rails
  • In loopback documentation what does variable 'cb' stands for?
  • Getting the scrolling offset when storing coordinates
  • Bigquery event streaming and table creation
  • TFS 2015 - Waiting for an agent to be requested
  • xcode don't localize specific strings
  • Subclassing QGraphicsItem prevents me from being able to use itemAt() on a QGraphicsScene/View
  • JqueryMobile Popup menu is not working
  • Simulate click Geckofx vb,net
  • how to upload multiple files in c# windows application
  • MS Access - How to change the linked table path by amend the table
  • presentShareDialogWithParams posts to FB wall, but callback handler results say error
  • How to handle images sent by a mobile device?
  • Android - Material Design - NavigationView - How to put vertical scroll?
  • AJAX Html Editor Extender upload image appearing blank
  • d3 v4 drag and drop with TypeScript
  • Swift: Switch statement fallthrough behavior
  • JQuery Internet Explorer and ajaxstop
  • java.lang.NoClassDefFoundError: com.parse.Parse$Configuration$Builder on below Lollipop versions
  • Opengl-es onTouchEvents problem or a draw problem? [closed]
  • Which linear programming package should I use for high numbers of constraints and “warm starts” [clo
  • Cannot Parse HTML Data Using Android / JSOUP
  • Codeigniter doesn't let me update entry, because some fields must be unique
  • Linking SubReports Without LinkChild/LinkMaster