#2211 closed defect (fixed)
[patch] exact, gt, gte, lt, lte, ne lookups fail for DateField fields
Reported by: | Owned by: | Adrian Holovaty | |
---|---|---|---|
Component: | Metasystem | Version: | dev |
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 |
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 (1)
Change History (4)
by , 18 years ago
Attachment: | datefield_lookup.diff added |
---|
See also #1754. Not sure which patch is better at the moment, since they both have good and bad sides.