Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

python - Django admin: timezone display

So i am making an app where you can find activities which happen at locations.

On the django-admin page i want to be able to modify Activities (which works).

However an activity has a starting time - i want this starting time to be in the same timezone as the location.

So i want it to display the start time, on the activity admin page, in the same timezone as the location is in, but then when saved it should be converted to UTC time.

The starttime is in an inline-formset, as it can have multiple start times.

I find a way to change the datetime when saving the objects, but i cant find a way to modify it when it's rendered in the inline-thing.

How do i modify data as it's rendered in the admin page?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

1 Answer

0 votes
by (71.8m points)

"So i want it to display the start time, on the activity admin page, in the same timezone as the location is in, but then when saved it should be converted to UTC time."

According to Django's documentation on Time zone aware input in forms (https://docs.djangoproject.com/en/1.10/topics/i18n/timezones/#time-zone-aware-input-in-forms):

When you enable time zone support, Django interprets datetimes entered in forms in the current time zone and returns aware datetime objects in cleaned_data.

Which from what I understood is what you want. This leads us to Default time zone and current time zone (https://docs.djangoproject.com/en/1.10/topics/i18n/timezones/#default-current-time-zone), which states:

The current time zone is the time zone that’s used for rendering.

You should set the current time zone to the end user’s actual time zone with activate(). Otherwise, the default time zone is used.

So, use activate() (https://docs.djangoproject.com/en/1.10/ref/utils/#django.utils.timezone.activate) to set timezone argument and you're good to go.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
...