I have a field in my models.py that accepts choices determined in a class:
from apps.users.constants import UserChoices
class User(models.Model):
choices = models.CharField(max_length=10, blank=True, choices=UserChoices.choices(), default=UserChoices.PUBLIC_USER)
The choice class is this:
from django.utils.translation import ugettext_lazy as _
class UserChoices:
PRIVATE_USER = "private_user"
PUBLIC_USER = "public_user"
@classmethod
def choices(cls):
return (
(cls.PRIVATE_USER, _("Private User")),
(cls.PUBLIC_USER, _("Public User")),
)
My doubt is how can I inherit this UserChoices class to another choice class, in order to extend it with another options.
I tried the following:
class ExtendedChoices(UserChoices):
OTHER_CHOICE = "other_choice"
@classmethod
def choices(cls):
return (
UserChoices.choices(),
(cls.OTHER_CHOICE, _("Other choice")),
)
But it gives me a migration error:
users.OtherModel.other_choice: (fields.E005) 'choices' must be an iterable containing (actual value, human readable name) tuples.
Obviously this example is simplified, the actual code has 40+ choices on the original class and 20+ in the extended one.