About Choices of Geographic Lists
When adding fields like country
or timezone
to your models, don't set the choices
argument to a static list or tuple. Instead use a callable (e.g. a function that returns a list of choices). This way, Django won't require to create new migrations when list items change because due to political updates like a country name change or a timezone adjustment.
import zoneinfo
from django.contrib.auth.models import AbstractUser
def get_timezone_choices():
return [(tz, tz) for tz in sorted(zoneinfo.available_timezones())]
class User(AbstractUser):
timezone = models.CharField(
max_length=50, choices=get_timezone_choices, default="UTC"
)
Also by me
Django Paddle Subscriptions app
For Django-based SaaS projects.
Django App for You
Django GDPR Cookie Consent app
For Django websites that use cookies.
Django App for You