class MyTypes(TextChoices):
MINE_AAA = '1', 'a',
MINE_BBB = '2', 'b',
MINE_CCC = '3', 'c',
MINE_DDD = '4', 'd',
MINE_EEE = '5', 'e'
my_type = models.CharField(max_length=1, choices=MyTypes.choices)
I have been struggling a lot with being able to use Choices in my models. With the above example, if I create an instance "my_obj" and set "my_obj.my_type" to "MyTypes.MINE_BBB", the if I look at the "my_obj", its type is MyTypes. However, if I later query "my_obj" and look at "my_obj.my_type", its type is now a string.
So, when I create the object initially and serialize it, the serializer needs to handle MyTypes, but if I serialize the value after a query, the serializer needs to handle a string.
What I was really expecting when using a TextChoices field was that the value stored in the Database would be the single character, but when I reference it in code, I would see my MyTypes.
Are we not supposed to get the TextChoices instance back when we reference the model? If not, what is the simplest way to get it? It seem that if I after iterate through the MyTypes.choices to get it, that it really doesn't provide much value.
Here is what the documentation looks like.
class YearInSchool(models.TextChoices):
FRESHMAN = 'FR', _('Freshman')
SOPHOMORE = 'SO', _('Sophomore')
JUNIOR = 'JR', _('Junior')
SENIOR = 'SR', _('Senior')
GRADUATE = 'GR', _('Graduate')