Fix: correct api created coercion with timezone (#10287)

This commit is contained in:
shamoon
2025-06-28 21:39:14 -07:00
committed by GitHub
parent a9085c65c5
commit 735681d294
2 changed files with 30 additions and 5 deletions

View File

@@ -18,7 +18,11 @@ from django.core.validators import MaxLengthValidator
from django.core.validators import RegexValidator
from django.core.validators import integer_validator
from django.utils.crypto import get_random_string
from django.utils.dateparse import parse_datetime
from django.utils.text import slugify
from django.utils.timezone import get_current_timezone
from django.utils.timezone import is_naive
from django.utils.timezone import make_aware
from django.utils.translation import gettext as _
from drf_spectacular.utils import extend_schema_field
from drf_spectacular.utils import extend_schema_serializer
@@ -972,11 +976,11 @@ class DocumentSerializer(
and ":" in data["created"]
):
# Handle old format of isoformat datetime string
try:
data["created"] = datetime.fromisoformat(data["created"]).date()
except ValueError: # pragma: no cover
# Just pass, validation will catch it
pass
parsed = parse_datetime(data["created"])
if parsed:
if is_naive(parsed):
parsed = make_aware(parsed, get_current_timezone())
data["created"] = parsed.astimezone().date()
return super().to_internal_value(data)
def validate(self, attrs):