#28356: Serializer got error when datetime field receive string instead datetime object -------------------------------------+------------------------------------- Reporter: | Owner: nobody Hermogenes Batista da Silva Filho | Type: Bug | Status: new Component: | Version: 1.11 Uncategorized | Severity: Normal | Keywords: serializer Triage Stage: | Has patch: 1 Unreviewed | Needs documentation: 0 | Needs tests: 0 Patch needs improvement: 0 | Easy pickings: 1 UI/UX: 0 | -------------------------------------+------------------------------------- Hi, I got an error, when I tried to serialize one model with DatetimeField and it have a string in valid format to parse it from datetime. I got it when I called `serializer.serializer`, one simplest case to reproduce it is that:
{{{ # models.py from django.db import models class SomeModel(models.Model): some_datetime = models.DateTimeField() }}} {{{ # Script which you want to serialize model from django.core import serializers from MyAPP.models import SomeModel some_model = SomeModel(some_datetime="2017-02-02T00:00:00") # We didn't have problem if you save it some_model.save() # But if you tried to serialize it, you got an error, because inside method # it expected `datetime` object and it will raise an exception # `AttributeError: 'str' object has no attribute 'isoformat'` serialized_object = serializers.serialize('json', [some_model]) }}} To fix it, we need to check if value is a string, and if have a valid format (using `parse_datetime` function), if is `None` we raise an exception (`ValidationError`). -- Ticket URL: <https://code.djangoproject.com/ticket/28356> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To post to this group, send email to django-updates@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/060.f78877106fb8e7475328487505f90290%40djangoproject.com. For more options, visit https://groups.google.com/d/optout.