[Django] #28356: Serializer got error when datetime field receive string instead datetime object

11 views
Skip to first unread message

Django

unread,
Jul 2, 2017, 10:52:03 AM7/2/17
to django-...@googlegroups.com
#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.

Django

unread,
Jul 2, 2017, 10:54:24 AM7/2/17
to django-...@googlegroups.com
#28356: Serializer got error when datetime field receive string instead datetime
object
-------------------------------------+-------------------------------------
Reporter: Hermogenes Batista | Owner:
da Silva Filho | Hermogenes Batista da Silva Filho
Type: Bug | Status: assigned
Component: Uncategorized | Version: 1.11
Severity: Normal | Resolution:
Keywords: serializer | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Hermogenes Batista da Silva Filho):

* owner: nobody => Hermogenes Batista da Silva Filho
* status: new => assigned


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

Django

unread,
Jul 2, 2017, 12:25:24 PM7/2/17
to django-...@googlegroups.com
#28356: Serializer got error when datetime field receive string instead datetime
object
-------------------------------------+-------------------------------------
Reporter: Hermogenes Batista | Owner:
da Silva Filho | Hermogenes Batista da Silva Filho
Type: Bug | Status: assigned
Component: Database layer | Version: 1.11
(models, ORM) |

Severity: Normal | Resolution:
Keywords: serializer | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* component: Uncategorized => Database layer (models, ORM)


Comment:

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?

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

Django

unread,
Jul 2, 2017, 1:35:53 PM7/2/17
to django-...@googlegroups.com
#28356: Serializer got error when datetime field receive string instead datetime
object
-------------------------------------+-------------------------------------
Reporter: Hermogenes Batista | Owner:
da Silva Filho | Hermogenes Batista da Silva Filho
Type: Bug | Status: assigned
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution:
Keywords: serializer | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0

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

Comment (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.

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

Django

unread,
Jul 2, 2017, 1:47:20 PM7/2/17
to django-...@googlegroups.com
#28356: Serializer got error when datetime field receive string instead datetime
object
-------------------------------------+-------------------------------------
Reporter: Hermogenes Batista | Owner:
da Silva Filho | Hermogenes Batista da Silva Filho
Type: Bug | Status: assigned
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution:
Keywords: serializer | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0

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

Comment (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).

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

Django

unread,
Jul 3, 2017, 9:13:06 AM7/3/17
to django-...@googlegroups.com
#28356: Serializer got error when datetime field receive string instead datetime
object
-------------------------------------+-------------------------------------
Reporter: Hermogenes Batista | Owner:
da Silva Filho | Hermogenes Batista da Silva Filho
Type: Bug | Status: closed

Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution: wontfix

Keywords: serializer | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

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


Comment:

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.

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

Reply all
Reply to author
Forward
0 new messages