question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DateTimeField should return a datetime instance

See original GitHub issue

DateTimeField 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:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
coleifercommented, Dec 27, 2020

Don’t store tz info. Just use naive datetimes in utc.

0reactions
yuriesclcommented, Dec 27, 2020

That worked, thanks. There should be an error thrown when trying to save with tzinfo saying it’s not supported.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found