I am trying to use the new Enum types in the latest Django version for the choice field. Specifically I am trying to store the various states of United States as follows:
class States(models.TextChoices):
ALABAMA = 'AL', 'Alabama'
ALASKA = 'AK', 'Alaska'
.....
.....
WISCONSIN = 'WI', 'Wisconsin'
WYOMING = 'WY', 'Wyoming'
class PersonalInfo(models.Model):
state = models.CharField(max_length=2, choices=States.choices, default=States.ALABAMA)
Works as expected. Now, I am also trying to make the max_length variable also a class attribute of the choice class to make the code more modular by doing the following:
class States(models.TextChoices):
ALABAMA = 'AL', 'Alabama'
ALASKA = 'AK', 'Alaska'
.....
.....
WISCONSIN = 'WI', 'Wisconsin'
WYOMING = 'WY', 'Wyoming'
MAX_LENGTH = 2
class PersonalInfo(models.Model):
state = models.CharField(max_length=States.MAX_LENGTH, choices=States.choices, default=States.ALABAMA)
This gives me an error as follows:
if self.max_length is not None and choice_max_length > self.max_length:
TypeError: '>' not supported between instances of 'int' and 'States'
I understand that Django also provides an alternative IntegerChoices for integer but how do I use both text and integer choices together.