Opened 13 years ago

Closed 13 years ago

Last modified 12 years ago

#2211 closed defect (fixed)

[patch] exact, gt, gte, lt, lte, ne lookups fail for DateField fields

Reported by: paolo@… Owned by: Adrian Holovaty
Component: Metasystem Version: master
Severity: normal Keywords:
Cc: paolo@… Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Documentation about field lookups cites this example: Entry.objects.filter(pub_date__lte='2006-01-01')

However, if pub_date is a DateField field the query fails. Same thing with exact, gt, gte, lt, lte, ne lookups.
Using Entry.objects.filter( all works fine, though I don't think this is the intended way to make this kind of query.

In the admin site also there are some troubles caused by this behaviour. Try to add a DateField field in list_filter for some model and ask for objects in 'Past 7 days'.

This is the traceback.

Traceback (most recent call last):
File "/home/paolo/django_current/django/core/handlers/" in get_response
  74. response = callback(request, *callback_args, **callback_kwargs)
File "/home/paolo/django_current/django/contrib/admin/views/" in _checklogin
  54. return view_func(request, *args, **kwargs)
File "/home/paolo/django_current/django/views/decorators/" in _wrapped_view_func
  40. response = view_func(request, *args, **kwargs)
File "/home/paolo/django_current/django/contrib/admin/views/" in change_list
  734. cl = ChangeList(request, model)
File "/home/paolo/django_current/django/contrib/admin/views/" in __init__
  565. self.get_results(request)
File "/home/paolo/django_current/django/contrib/admin/views/" in get_results
  624. result_list = list(self.query_set)
File "/home/paolo/django_current/django/db/models/" in __iter__
  94. return iter(self._get_data())
File "/home/paolo/django_current/django/db/models/" in _get_data
  412. self._result_cache = list(self.iterator())
File "/home/paolo/django_current/django/db/models/" in iterator
  162. select, sql, params = self._get_sql_clause()
File "/home/paolo/django_current/django/db/models/" in _get_sql_clause
  426. tables2, joins2, where2, params2 = self._filters.get_sql(opts)
File "/home/paolo/django_current/django/db/models/" in get_sql
  557. tables2, joins2, where2, params2 = val.get_sql(opts)
File "/home/paolo/django_current/django/db/models/" in get_sql
  606. return parse_lookup(self.kwargs.items(), opts)
File "/home/paolo/django_current/django/db/models/" in parse_lookup
  716. tables2, joins2, where2, params2 = lookup_inner(path, clause, value, opts, opts.db_table, None)
File "/home/paolo/django_current/django/db/models/" in lookup_inner
  876. params.extend(field.get_db_prep_lookup(clause, value))
File "/home/paolo/django_current/django/db/models/fields/" in get_db_prep_lookup
  415. value = value.strftime('%Y-%m-%d')

  AttributeError at /admin/tessere/tessera/
  'str' object has no attribute 'strftime'

Attachments (1)

datefield_lookup.diff (549 bytes) - added by paolo <paolo@…> 13 years ago.

Download all attachments as: .zip

Change History (4)

Changed 13 years ago by paolo <paolo@…>

Attachment: datefield_lookup.diff added

comment:1 Changed 13 years ago by Malcolm Tredinnick

See also #1754. Not sure which patch is better at the moment, since they both have good and bad sides.

comment:2 Changed 13 years ago by deryck@…

As if two patches for the same issue weren't enough, See #2192.

comment:3 Changed 13 years ago by Malcolm Tredinnick

Resolution: fixed
Status: newclosed

(In [3223]) Fixed #1754, #2211, #2192 -- allow date filtering comparisons to use strings as
well as date objects. Fixed a couple of admin crashes as well.

Note: See TracTickets for help on using tickets.
Back to Top