DateTimeField should return a datetime instance
See original GitHub issueDateTimeField
should return a datetime
instance when accessed instead of a str
instance. I imagine one of the main reasons to use a DateTimeField
instead of CharField
is to work with the field as a datetime
instance without worrying about converting to/from str
.
Model:
class Entry(Model):
created_at = DateTimeField(default=datetime.now(timezone.utc))
Expected behavior:
entry = Entry.get()
print(type(t.created_at))
<class 'datetime.datetime'>
Actual behavior:
entry = Entry.get()
print(type(t.created_at))
<class 'str'>
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
DateTimeField - Django Models - GeeksforGeeks
DateTimeField is a date and time field which stores date, represented in Python by a datetime.datetime instance. As the name suggests, ...
Read more >Get date from a Django DateTimeField - python - Stack Overflow
DateTimeField becomes a datetime.datetime object in Python ... This works because Django will translate the DateTimeField into the Python type ...
Read more >Django Tips #4 Automatic DateTime Fields
Both Django's DateTimeField and DateField have two very useful arguments for automatically managing date and time.
Read more >Python Dates - W3Schools
Directive Description Example Try it
%a Weekday, short version Wed Try it »
%A Weekday, full version Wednesday Try it »
%w Weekday as a number...
Read more >Form fields - Django documentation
Takes a list of valid values and returns a “compressed” version of those values – in a single value. For example, SplitDateTimeField is...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Don’t store tz info. Just use naive datetimes in utc.
That worked, thanks. There should be an error thrown when trying to save with tzinfo saying it’s not supported.