[Django] #31471: DateTimeField is returned as str instead of as datetime object, when using reverse lookup

10 views
Skip to first unread message

Django

unread,
Apr 16, 2020, 6:24:51 AM4/16/20
to django-...@googlegroups.com
#31471: DateTimeField is returned as str instead of as datetime object, when using
reverse lookup
-------------------------------------+-------------------------------------
Reporter: Fonville | Owner: nobody
Type: Bug | Status: new
Component: | Version: 3.0
Uncategorized |
Severity: Normal | Keywords: datetime, OneToOne
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
I have a Telemetry class/model with a DateTimeField:

{{{
class Telemetry(models.Model):
time_stamp = models.DateTimeField()
}}}
Whitin this class/model the time_stamp field is correctly treated as a
Python `datetime` object


Telemetry has a OnetoOne relationship with another model:
{{{
class ExtraTelemetry(models.Model):
telemetry = models.OneToOneField(Telemetry, on_delete=models.CASCADE,
primary_key=True)
}}}

in this related ExtraTelemetery object I am fetching the value of the
DateTimeField with:
`self.telemetry.time_stamp`

You'd expect this to be a Python `datetime` object too, but instead it
returns a `str` object (e.g. `"2020-04-15T17:03:05Z"`)
I am only able to use the field as datetime object by parsing it myself
with `dateutil.parser.parse(self.telemetry.time_stamp)`

I think this is a bug, because it should be a datetime object and not a
string, even when fetched through a relationship.
Django version is 3.0.5 with Python3.8.2 and SQLite

--
Ticket URL: <https://code.djangoproject.com/ticket/31471>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Apr 16, 2020, 3:30:13 PM4/16/20
to django-...@googlegroups.com
#31471: DateTimeField is returned as str instead of as datetime object, when using
reverse lookup
------------------------------------+--------------------------------------
Reporter: Fonville | Owner: ovkulkarni
Type: Bug | Status: assigned
Component: Uncategorized | Version: 3.0
Severity: Normal | Resolution:
Keywords: datetime, OneToOne | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0

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

* owner: nobody => ovkulkarni
* status: new => assigned


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

Django

unread,
Apr 16, 2020, 3:54:26 PM4/16/20
to django-...@googlegroups.com
#31471: DateTimeField is returned as str instead of as datetime object, when using
reverse lookup
------------------------------------+--------------------------------------
Reporter: Fonville | Owner: (none)
Type: Bug | Status: new

Component: Uncategorized | Version: 3.0
Severity: Normal | Resolution:
Keywords: datetime, OneToOne | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0

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

* owner: ovkulkarni => (none)
* status: assigned => new


Comment:

I was unable to reproduce this bug using Django 3.0.5, Python 3.8.0, and a
SQLite database. My models and REPL output are shown below:
models.py
{{{
from django.db import models
class Test(models.Model):
time_stamp = models.DateTimeField(auto_now=True)

class RelationTest(models.Model):
test_field = models.OneToOneField(Test, on_delete=models.CASCADE,
primary_key=True)
}}}
REPL:
{{{
$ ./manage.py shell
Python 3.8.0 (v3.8.0:fa919fdf25, Oct 14 2019, 10:23:27)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from app.models import *
>>> r = RelationTest.objects.get(test_field=1)
>>> r.test_field.time_stamp
datetime.datetime(2020, 4, 16, 19, 48, 44, 277170, tzinfo=<UTC>)
}}}

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

Django

unread,
Apr 16, 2020, 3:57:36 PM4/16/20
to django-...@googlegroups.com
#31471: DateTimeField is returned as str instead of as datetime object, when using
reverse lookup
------------------------------------+--------------------------------------
Reporter: Fonville | Owner: (none)
Type: Bug | Status: closed
Component: Uncategorized | Version: 3.0
Severity: Normal | Resolution: worksforme
Keywords: datetime, OneToOne | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0

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

* status: new => closed
* resolution: => worksforme


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

Django

unread,
Apr 16, 2020, 5:47:21 PM4/16/20
to django-...@googlegroups.com
#31471: DateTimeField is returned as str instead of as datetime object, when using
reverse lookup
------------------------------------+--------------------------------------
Reporter: Fonville | Owner: (none)
Type: Bug | Status: closed
Component: Uncategorized | Version: 3.0
Severity: Normal | Resolution: worksforme
Keywords: datetime, OneToOne | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0

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

Comment (by Fonville):

Thanks for testing in a simple test case, unfortunately I am still hitting
the bug in the context of the app I am writing.
It is triggered during the running of a post_save receiver. I will
investigate a bit more, what factor in the context is relevant and how to
reproduce it!

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

Django

unread,
Apr 17, 2020, 6:18:13 PM4/17/20
to django-...@googlegroups.com
#31471: update_or_create object returns either an unparsed field (like a str) or
datetime for DateTimeField, depending on whether object is saved or not.

------------------------------------+--------------------------------------
Reporter: Fonville | Owner: (none)
Type: Bug | Status: closed
Component: Uncategorized | Version: 3.0
Severity: Normal | Resolution: worksforme
Keywords: datetime, OneToOne | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0

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

Comment (by Fonville):

Ok, I found the culprit, I was entering the datetime as a str into the
model with the DateTimeField using a update_or_create() method as a kwarg.

Depending on whether the update_or_create had triggered a save (and thus
parsing) of the field, the field was either a string or a datetime.

I am not sure what your take is on things, but imho the nondeterministic
behaviour of update_or_create() is in my opinion not expected by a
developer.
I would expect even if the object is not updated in the database, the
field to still be parsed into a datetime in the object returned by the
update_or_create() method.

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

Django

unread,
Apr 18, 2020, 10:31:42 AM4/18/20
to django-...@googlegroups.com
#31471: update_or_create object returns either an unparsed field (like a str) or
datetime for DateTimeField, depending on whether object is saved or not.
-------------------------------------+-------------------------------------

Reporter: Fonville | Owner: (none)
Type: Bug | Status: closed
Component: Database layer | Version: 3.0
(models, ORM) | Resolution:
Severity: Normal | worksforme

Keywords: datetime, OneToOne | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

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

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


Comment:

While I haven't investigated the details here, I'm inclined to say that
behavior probably won't change. See, for example, past tickets like
#12401.

--
Ticket URL: <https://code.djangoproject.com/ticket/31471#comment:6>

Django

unread,
Apr 20, 2020, 7:37:13 AM4/20/20
to django-...@googlegroups.com
#31471: update_or_create object returns either an unparsed field (like a str) or
datetime for DateTimeField, depending on whether object is saved or not.
-------------------------------------+-------------------------------------
Reporter: Fonville | Owner: (none)
Type: Bug | Status: closed
Component: Database layer | Version: 3.0
(models, ORM) | Resolution:
Severity: Normal | worksforme
Keywords: datetime, OneToOne | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

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

Comment (by Fonville):

Thanks for your explanation with the past ticket.
Maybe it is good to document somewhere these kind of gotchas that
(underwater) `.save()` can change the fieldtypes of the model object.

--
Ticket URL: <https://code.djangoproject.com/ticket/31471#comment:7>

Reply all
Reply to author
Forward
0 new messages