Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

save time zone in Django Models

I am creating a form in Django. I have to put a input type field, which only stores the timezone in database(which will be chosen by a user from a drop Down list at form). I am not able to find out any approach to create this timezone model and how it will return the local time according to saved timezone. I choose following field but it stores also minute hours and second also

 timestamp = models.DateTimeField() 

Drop down list should be in form of : ... GMT +5:30 GMT +6:00...and so on.

like image 975
Amit Pal Avatar asked Feb 26 '12 15:02

Amit Pal


People also ask

How does Django store time zones?

Neither django nor python provide a set of timezones for you to use. For that, you will need an additional module like pytz . You can get a list of all timezones like this: >>> import pytz >>> pytz.

How do I set the time in Django?

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.

What is Use_tz in Django?

When USE_TZ is False, this is the time zone in which Django will store all datetimes. When USE_TZ is True, this is the default time zone that Django will use to display datetimes in templates and to interpret datetimes entered in forms.


1 Answers

I think the above answers are all correct, but I leave mine here as an simple example...

class UserProfile(models.Model):        import pytz     TIMEZONES = tuple(zip(pytz.all_timezones, pytz.all_timezones))      # ...     timezone = models.CharField(max_length=32, choices=TIMEZONES,      default='UTC') # ... 
like image 84
Slipstream Avatar answered Sep 30 '22 14:09

Slipstream