Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you make Django-admin timezone aware?

I have datetime information stored in the database as UTC. I'm displaying this info using Django admin.

Is there a way to display datetime information to the end-user's current timezone in Django admin?

Usually I think the logic for this would be in the template/view layer, however, Django-admin hides this all away from you and it seems like I would have to override many of the hidden files (which I don't want to do).

One idea is to subclass DateTimeField in the models and make it so that it converts it to local timezone whenever you grab it from the database, though I'm not sure if this will work.

Anyone have any suggestions? Thanks.

like image 961
keyworks Avatar asked Mar 13 '12 18:03

keyworks


People also ask

How do you make a timezone aware datetime object in Python?

You can also use the pytz module to create timezone-aware objects. For this, we will store the current date and time in a new variable using the datetime. now() function of datetime module and then we will add the timezone using timezone function of pytz module.

What is the default time zone setting in Django?

Django's timezone is set to UTC by default.

What are the time zone utilities used in Django?

utils. tzinfo , is a support system within Django that stores data and time information in UTC in the database. It uses the time-zone-aware- datetime objects internally and translates them to the user's time zone in templates.

How does Django store current date and time?

First, open the views.py file of your Django application and import the datetime module. Next, use the datetime. now() method to get the current date and time value.


2 Answers

Django 1.4 will introduce timezone support but the admin will still not be using it yet. There are still questions around which timezone implementation to "bless" (pytz), where to store the timezone per user (without built-in schema migration auth.User basically can't get touched without forcing every Django user to sync their database), and there isn't a UI for timezone selection yet.

So the parts are there but it's still DIY at this stage, sorry.

See the thread I started on django-developers http://groups.google.com/group/django-developers/browse_thread/thread/1386a2bf73babc6a

like image 112
Danny W. Adair Avatar answered Oct 24 '22 18:10

Danny W. Adair


Localized timezone support is available in the soon-to-be-released Django 1.4.

like image 34
roam Avatar answered Oct 24 '22 20:10

roam