Django: Do ​​I have to convert a signed datetime instance to UTC before sending to the model?

I have knowledgeable datetime instances (where tzinfo = "America/Los_Angeles") that I would like to keep in the model.

Do I have to convert it to UTC somehow before saving? Or can I just keep it as it is, since it knows about its own time zone? Should I convert it to user time zones later using activate(), or will Django do this for me as the instance knows

I'm most curious about the conventions. Thanks in advance.

+4
source share
2 answers

If it is a time zone and you are USE_TZset to Trueyour own settings.py, django will automatically convert it to UTC when you save it.

When you select it from the database later, it will be notified of the time zone, but set to UTC: django will not save the time zone that it used.

When you display the date-time in the template, it will be converted to the time zone set TIME_ZONEin yours settings.py, unless you use django time zone utilities to activate a different time zone.

+4
source

UTC , Django . , , TIMEZONE.

activate() , django.utils.timezone.get_current_timezone().

, , , datetime, . , .

, activate() , DateTimeField. .

override().

, , datetime , activate() request.user. / , Django , , .

+1

Source: https://habr.com/ru/post/1530113/


All Articles