How to store timezones efficiently in Django model?

Viewed 685

I have a timezone field in my Django model :

import pytz
ALL_TIMEZONES = sorted((item, item) for item in pytz.all_timezones)
...
class Snippet(models.Model):
    tz = models.CharField(choices=ALL_TIMEZONES,max_length=32)

I'm a little bit concerned about the space occupied by the tz field because I expect to have many many snippets in the future. Longest timezone is 32 characters long but there are only 593 timezones so 2 bytes would be sufficient to store the timezone.

Is there a better way to store/define my tz field? Of course I could use my own coding scheme but before I want to make sure there is no other solution.

1 Answers

1 - I think it's a good option store as "America/Sao_Paulo". I do not think there is a numerical ID for time zones.

2 - The size consumed by a CharField is related to your database engine. Most databases only consumes the space required to store the text value. There is no difference in terms of space to set 32 or 320 characters (I think 64 is a good size).

3 - Use the third normal form: create a timezone table, then uses fks to link it:

class MytimezoneTable(models.Model):
    tz_name = models.CharField(choices=ALL_TIMEZONES, max_length=64)

class Snippet(models.Model):
    tz = models.ForeignKey(MyTimezoneTable)
Related