Model Fields¶
TimeZoneField¶
-
class
djem.models.TimeZoneField(**kwargs)[source]¶ TimeZoneFieldis a model field that stores timezone name strings (‘Australia/Sydney’, ‘US/Eastern’, etc) in the database and provides access toTimeZoneHelperinstances for the stored timezones.TimeZoneFieldwill only store valid timezone strings, or a null value ifnull=True. It will not store arbitrary strings, including the empty string.The default form field is a
TypedChoiceFieldwith aSelectwidget.TimeZoneFieldprovides default values for the following constructor arguments:-
choices¶ Defaults to a list of 2-tuples containing the timezones provided by pytz.common_timezones. Both items of each 2-tuple simply contain the timezone name. This is equivalent to:
choices = [(tz, tz) for tz in pytz.common_timezones]
If passing in a custom list of choices, it must match this format. The default value is stored on
TimeZoneFieldin theCHOICESconstant.
-
max_length¶ Defaults to 63. This default value is stored on
TimeZoneFieldin theMAX_LENGTHconstant.
Example, using
TimeZoneFieldon a custom User model:# models.py from django.contrib.auth.models import AbstractBaseUser from djem.models import TimeZoneField class CustomUser(AbstractBaseUser): ... time_zone = TimeZoneField()
>>> user = CustomUser.objects.filter(timezone='Australia/Sydney').first() >>> user.timezone <TimeZoneHelper: Australia/Sydney>
Note
Use of
TimeZoneFieldrequires pytz to be installed. It will raise an exception during instantiation ifpytzis not available.Note
Use of
TimeZoneFieldonly makes sense if USE_TZ is True.See also
The
djem.forms.TimeZoneFieldform field.-