Opened 7 years ago

Closed 7 years ago

#29395 closed Bug (duplicate)

Assigning correctly formatted time string to TimeField field works, but leaves the field value a string (and not datetime.time as when retrieved)

Reported by: Tomer Zekharya Owned by: nobody
Component: Database layer (models, ORM) Version: 2.0
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When initializing a TimeField field with a well formatted time string (i.e '12:00:00') and saving the object, it works fine, but leaves the field as 'str' type and does not convert it to a datetime.time object. This creates an inconsistency, where newly saved objects may have 'str' values for TimeField fields, while retrieved objects have datetime.time values. The docs clearly state that TimeField field in Django is represented as datetime.time object in python.

For example:

models.py

from django.db import models

class A(models.Model):
    time = models.TimeField()

And when using this model as described above:

>>> t = A(time='12:10:00')
>>> t.save()
>>> type(t.time)
<class 'str'>

>>> t2 = A.objects.get(id=t.id)
>>> type(t2.time)
<class 'datetime.time'>

Change History (1)

comment:1 by Tim Graham, 7 years ago

Resolution: duplicate
Status: newclosed

Duplicate of #27825 (the consensus is to document the limitation).

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