mirror of
https://github.com/django/django.git
synced 2024-12-22 09:05:43 +00:00
Fixed #34921 -- Fixed crash of warning for unbound naive datetimes.
This commit is contained in:
parent
61cc0e6f2c
commit
b5311ee232
@ -1595,10 +1595,13 @@ class DateTimeField(DateField):
|
|||||||
# local time. This won't work during DST change, but we can't
|
# 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
|
# do much about it, so we let the exceptions percolate up the
|
||||||
# call stack.
|
# call stack.
|
||||||
|
try:
|
||||||
|
name = f"{self.model.__name__}.{self.name}"
|
||||||
|
except AttributeError:
|
||||||
|
name = "(unbound)"
|
||||||
warnings.warn(
|
warnings.warn(
|
||||||
"DateTimeField %s.%s received a naive datetime "
|
f"DateTimeField {name} received a naive datetime ({value}) while "
|
||||||
"(%s) while time zone support is active."
|
"time zone support is active.",
|
||||||
% (self.model.__name__, self.name, value),
|
|
||||||
RuntimeWarning,
|
RuntimeWarning,
|
||||||
)
|
)
|
||||||
default_timezone = timezone.get_default_timezone()
|
default_timezone = timezone.get_default_timezone()
|
||||||
|
@ -10,6 +10,7 @@ from django.contrib.auth.models import User
|
|||||||
from django.core import serializers
|
from django.core import serializers
|
||||||
from django.db import connection
|
from django.db import connection
|
||||||
from django.db.models import F, Max, Min
|
from django.db.models import F, Max, Min
|
||||||
|
from django.db.models.functions import Now
|
||||||
from django.http import HttpRequest
|
from django.http import HttpRequest
|
||||||
from django.template import (
|
from django.template import (
|
||||||
Context,
|
Context,
|
||||||
@ -327,6 +328,13 @@ class NewDatabaseTests(TestCase):
|
|||||||
event = Event.objects.get()
|
event = Event.objects.get()
|
||||||
self.assertEqual(event.dt, datetime.datetime(2011, 9, 1, tzinfo=EAT))
|
self.assertEqual(event.dt, datetime.datetime(2011, 9, 1, tzinfo=EAT))
|
||||||
|
|
||||||
|
@requires_tz_support
|
||||||
|
def test_filter_unbound_datetime_with_naive_date(self):
|
||||||
|
dt = datetime.date(2011, 9, 1)
|
||||||
|
msg = "DateTimeField (unbound) received a naive datetime"
|
||||||
|
with self.assertWarnsMessage(RuntimeWarning, msg):
|
||||||
|
Event.objects.annotate(unbound_datetime=Now()).filter(unbound_datetime=dt)
|
||||||
|
|
||||||
@requires_tz_support
|
@requires_tz_support
|
||||||
def test_naive_datetime_with_microsecond(self):
|
def test_naive_datetime_with_microsecond(self):
|
||||||
dt = datetime.datetime(2011, 9, 1, 13, 20, 30, 405060)
|
dt = datetime.datetime(2011, 9, 1, 13, 20, 30, 405060)
|
||||||
|
Loading…
Reference in New Issue
Block a user