dearprudence
dearprudence

Reputation: 157

Adding custom date to DateTimeField in Django?

Say I have a model with a field as such:

begin_date = models.DateTimeField(blank=True)

Whenever I want to add a date using a string, for example, '2020-06-15T11:00' it gives me the RuntimeWarning: received a naive datetime while time zone support is active.

So what's the proper way of adding date to a model in Django?

Upvotes: 2

Views: 1039

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477676

If you add a 'Z' suffix, you specify the datetime in UTC, for the ISO 8601 standard [Django-doc]:

'2020-06-15T11:00Z'

You can however also pass a datetime object:

from datetime import datetime
from pytz import UTC

datetime(2020, 6, 15, 11, 00, tzinfo=UTC)

Upvotes: 3

Related Questions