[Django] #34921: Filtering an unbound DateTimeField with naive date crashes

22 views
Skip to first unread message

Django

unread,
Oct 22, 2023, 2:55:24 AM10/22/23
to django-...@googlegroups.com
#34921: Filtering an unbound DateTimeField with naive date crashes
-------------------------------------+-------------------------------------
Reporter: David | Owner: nobody
Sanders |
Type: Bug | Status: new
Component: Database | Version: dev
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
It's possible to crash the naive datetime warning in
`DateTimeField.to_python()` by filtering an unbound datetime with a naive
timstamp:

{{{
class EmptyModel(Model):
pass

EmptyModel.objects.annotate(unbound=Now()).filter(unbound__gte=date(2000,
1, 1))
}}}

The resulting exception is pasted below.

The cause is the unsuccessful access of attribute `self.model` in
`DateTimeField.to_python()`:

{{{
warnings.warn(
"DateTimeField %s.%s received a naive datetime "
"(%s) while time zone support is active."
% (self.model.__name__, self.name, value),
RuntimeWarning,
)
}}}

`DateTime.get_prep_value()` handles this correctly by catching the
attribute error raised and using the name "unbound" instead:

{{{
try:
name = "%s.%s" % (self.model.__name__, self.name)
except AttributeError:
name = "(unbound)"
warnings.warn(
"DateTimeField %s received a naive datetime (%s)"
" while time zone support is active." % (name, value),
RuntimeWarning,
)
}}}

Exception details:

{{{
path/to/django/db/models/query.py:1476: in filter
return self._filter_or_exclude(False, args, kwargs)
path/to/django/db/models/query.py:1494: in _filter_or_exclude
clone._filter_or_exclude_inplace(negate, args, kwargs)
path/to/django/db/models/query.py:1501: in _filter_or_exclude_inplace
self._query.add_q(Q(*args, **kwargs))
path/to/django/db/models/sql/query.py:1599: in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
path/to/django/db/models/sql/query.py:1631: in _add_q
child_clause, needed_inner = self.build_filter(
path/to/django/db/models/sql/query.py:1499: in build_filter
condition = self.build_lookup(lookups, reffed_expression, value)
path/to/django/db/models/sql/query.py:1375: in build_lookup
lookup = lookup_class(lhs, rhs)
path/to/django/db/models/lookups.py:30: in __init__
self.rhs = self.get_prep_lookup()
path/to/django/db/models/lookups.py:88: in get_prep_lookup
return self.lhs.output_field.get_prep_value(self.rhs)
path/to/django/db/models/fields/__init__.py:1648: in get_prep_value
value = super().get_prep_value(value)
path/to/django/db/models/fields/__init__.py:1527: in get_prep_value
return self.to_python(value)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <django.db.models.fields.DateTimeField>, value =
datetime.datetime(2000, 1, 1, 0, 0)

def to_python(self, value):
if value is None:
return value
if isinstance(value, datetime.datetime):
return value
if isinstance(value, datetime.date):
value = datetime.datetime(value.year, value.month, value.day)
if settings.USE_TZ:
# For backwards compatibility, interpret naive datetimes
in
# local time. This won't work during DST change, but we
can't
# do much about it, so we let the exceptions percolate up
the
# call stack.
warnings.warn(
"DateTimeField %s.%s received a naive datetime "
"(%s) while time zone support is active."
> % (self.model.__name__, self.name, value),
RuntimeWarning,
)
E AttributeError: 'DateTimeField' object has no attribute
'model'

path/to/django/db/models/fields/__init__.py:1601: AttributeError
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/34921>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Oct 22, 2023, 3:43:27 AM10/22/23
to django-...@googlegroups.com
#34921: Filtering an unbound DateTimeField with naive date crashes
-------------------------------------+-------------------------------------
Reporter: David Sanders | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by David Sanders):

PR to make warning same as `get_prep_value()`:
https://github.com/django/django/pull/17399

--
Ticket URL: <https://code.djangoproject.com/ticket/34921#comment:1>

Django

unread,
Oct 22, 2023, 3:45:00 AM10/22/23
to django-...@googlegroups.com
#34921: Filtering an unbound DateTimeField with naive date crashes
-------------------------------------+-------------------------------------
Reporter: David Sanders | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by David Sanders:

Old description:

New description:

It's possible to crash the naive datetime warning in
`DateTimeField.to_python()` by filtering an unbound datetime with a naive

`date`:

{{{
class EmptyModel(Model):
pass

EmptyModel.objects.annotate(unbound=Now()).filter(unbound__gte=date(2000,
1, 1))
}}}

Exception details:

path/to/django/db/models/fields/__init__.py:1601: AttributeError
}}}

--

--
Ticket URL: <https://code.djangoproject.com/ticket/34921#comment:2>

Django

unread,
Oct 22, 2023, 6:55:33 AM10/22/23
to django-...@googlegroups.com
#34921: Filtering an unbound DateTimeField with naive date crashes
-------------------------------------+-------------------------------------
Reporter: David Sanders | Owner: David
| Sanders
Type: Bug | Status: assigned

Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* owner: nobody => David Sanders
* status: new => assigned
* has_patch: 0 => 1
* stage: Unreviewed => Accepted


Comment:

Thanks for the report.

--
Ticket URL: <https://code.djangoproject.com/ticket/34921#comment:3>

Django

unread,
Oct 22, 2023, 8:27:26 AM10/22/23
to django-...@googlegroups.com
#34921: Filtering an unbound DateTimeField with naive date crashes
-------------------------------------+-------------------------------------
Reporter: David Sanders | Owner: David
| Sanders
Type: Bug | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/34921#comment:4>

Django

unread,
Oct 22, 2023, 1:34:09 PM10/22/23
to django-...@googlegroups.com
#34921: Filtering an unbound DateTimeField with naive date crashes
-------------------------------------+-------------------------------------
Reporter: David Sanders | Owner: David
| Sanders
Type: Bug | Status: closed

Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"b5311ee23219cfb676e2e67667ecba1e5d363aa0" b5311ee]:
{{{
#!CommitTicketReference repository=""
revision="b5311ee23219cfb676e2e67667ecba1e5d363aa0"
Fixed #34921 -- Fixed crash of warning for unbound naive datetimes.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/34921#comment:5>

Django

unread,
Oct 22, 2023, 1:34:31 PM10/22/23
to django-...@googlegroups.com
#34921: Filtering an unbound DateTimeField with naive date crashes
-------------------------------------+-------------------------------------
Reporter: David Sanders | Owner: David
| Sanders
Type: Bug | Status: closed
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"4dec7eded9e58d7cefe54ea32137599cd4d1ea69" 4dec7ede]:
{{{
#!CommitTicketReference repository=""
revision="4dec7eded9e58d7cefe54ea32137599cd4d1ea69"
[5.0.x] Fixed #34921 -- Fixed crash of warning for unbound naive
datetimes.

Backport of b5311ee23219cfb676e2e67667ecba1e5d363aa0 from main
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/34921#comment:6>

Reply all
Reply to author
Forward
0 new messages