#28356 closed Bug (wontfix)

Serializer got error when datetime field receive string instead datetime object

Reported by: Hermogenes Batista da Silva Filho Owned by: Hermogenes Batista da Silva Filho
Component: Database layer (models, ORM) Version: 1.11
Severity: Normal Keywords: serializer
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no


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

# 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).

Change History (5)

comment:1 Changed 20 months ago by Hermogenes Batista da Silva Filho

Owner: changed from nobody to Hermogenes Batista da Silva Filho
Status: newassigned

comment:2 Changed 20 months ago by Tim Graham

Component: UncategorizedDatabase layer (models, ORM)

This doesn't strike me as a bug. I think your script should take care of parsing the string so a date or datetime is assigned. Is there a problem with that approach?

comment:3 Changed 20 months ago by Hermogenes Batista da Silva Filho

I thinking it like a bug, because when I was saving, this format (string instead date/datetime) is accepted and it will saved without problem, so this behavior must be equals on serializer too.

comment:4 Changed 20 months ago by Hermogenes Batista da Silva Filho

Also, when you save your instance with this value, and have implemented one post_save to serialize it and send to Kafka (for example), the instance received on signal, will have that field with string yet, so we got this problem (this is my scenario).

comment:5 Changed 20 months ago by Tim Graham

Resolution: wontfix
Status: assignedclosed

I'm still not convinced that Django should head down the path of trying to handle field types that are different from the type that's retrieved from the database (see #27825 for another case). Just because strings happen to work to saving a model instance doesn't mean that the Django ecosystem should be prepared to handle a string for these fields -- that'll add a lot of complexity. Also, the usual case for serializers is to handle objects from the database, not unsaved model instances, so my opinion is that fixing this in your code is more appropriate than adding more complexity to Django. If you still disagree, feel free to write to the DevelopersMailingList to get other opinions.

Note: See TracTickets for help on using tickets.
Back to Top