save time zone in Django Models save time zone in Django Models django django

save time zone in Django Models


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')# ...


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.all_timezones ['Africa/Abidjan', 'Africa/Accra', 'Africa/Addis_Ababa', 'Africa/Algiers', 'Africa/Asmara','Africa/Asmera'....

You can their store the timezone name in a CharField.

By the way, choosing a timezone by "GMT +6:00" is not a good idea. For example, EST is usually 5 hours behind GMT, but for 2 weeks around daylight savings time changes, the offset is different. Also, at some times of year someone in Queensland and someone in New South Wales both have the same GMT offset, but because NSW has DST and Queensland doesn't, for half the year their GMT offsets are different. The only safe way to list timezones is to list the actual geographic timezones.