save time zone in Django Models

前端 未结 4 1832
抹茶落季
抹茶落季 2021-01-03 20:15

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

4条回答
  •  一生所求
    2021-01-03 20:53

    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.

提交回复
热议问题