Django

Code

Ticket #2211 (closed: fixed)

Opened 2 years ago

Last modified 2 years ago

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

Reported by: paolo@php3.it Assigned to: adrian
Milestone: Component: Metasystem
Version: SVN Keywords:
Cc: paolo@php3.it Triage Stage: Unreviewed
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

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(pub_date__lte=datetime.date()) 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/base.py" in get_response
  74. response = callback(request, *callback_args, **callback_kwargs)
File "/home/paolo/django_current/django/contrib/admin/views/decorators.py" in _checklogin
  54. return view_func(request, *args, **kwargs)
File "/home/paolo/django_current/django/views/decorators/cache.py" in _wrapped_view_func
  40. response = view_func(request, *args, **kwargs)
File "/home/paolo/django_current/django/contrib/admin/views/main.py" in change_list
  734. cl = ChangeList(request, model)
File "/home/paolo/django_current/django/contrib/admin/views/main.py" in __init__
  565. self.get_results(request)
File "/home/paolo/django_current/django/contrib/admin/views/main.py" in get_results
  624. result_list = list(self.query_set)
File "/home/paolo/django_current/django/db/models/query.py" in __iter__
  94. return iter(self._get_data())
File "/home/paolo/django_current/django/db/models/query.py" in _get_data
  412. self._result_cache = list(self.iterator())
File "/home/paolo/django_current/django/db/models/query.py" in iterator
  162. select, sql, params = self._get_sql_clause()
File "/home/paolo/django_current/django/db/models/query.py" in _get_sql_clause
  426. tables2, joins2, where2, params2 = self._filters.get_sql(opts)
File "/home/paolo/django_current/django/db/models/query.py" in get_sql
  557. tables2, joins2, where2, params2 = val.get_sql(opts)
File "/home/paolo/django_current/django/db/models/query.py" in get_sql
  606. return parse_lookup(self.kwargs.items(), opts)
File "/home/paolo/django_current/django/db/models/query.py" 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/query.py" in lookup_inner
  876. params.extend(field.get_db_prep_lookup(clause, value))
File "/home/paolo/django_current/django/db/models/fields/__init__.py" in get_db_prep_lookup
  415. value = value.strftime('%Y-%m-%d')

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

Attachments

datefield_lookup.diff (0.5 kB) - added by paolo <paolo@php3.it> on 06/21/06 17:13:54.

Change History

06/21/06 17:13:54 changed by paolo <paolo@php3.it>

  • attachment datefield_lookup.diff added.

06/21/06 20:52:36 changed by mtredinnick

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

06/26/06 13:40:35 changed by deryck@samba.org

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

06/28/06 06:37:18 changed by mtredinnick

  • status changed from new to closed.
  • resolution set to fixed.

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


Add/Change #2211 ([patch] exact, gt, gte, lt, lte, ne lookups fail for DateField fields)




Change Properties
Action