Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 563
Casper Bang Avatar asked Aug 02 '16 20:08

Casper Bang


People also ask

How do I change the timezone in Django admin?

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.

How do I get current time zone in Django?

The solution to this problem is to use UTC in the code and use local time only when interacting with end users. Time zone support is disabled by default. To enable it, set USE_TZ = True in your settings file. In Django 5.0, time zone support will be enabled by default.

How do I change the date format in Django admin?

Changing django admin default formats could be done changing the django locale formats for every type you want. Put the following on your admin.py file (or settings.py) to change datetime default format at your django admin. It will change the ModelAdmin's datetime formats on that file (or whole site if in settings).


Video Answer


1 Answers

"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.

like image 71
Siegmeyer Avatar answered Oct 19 '22 14:10

Siegmeyer