Trick
use TypedChoiceField()
The answer to your search is to use TypedChoiceField , not ChoiceField .
you get a type field from django form using cleaned_data from ChoiceField . The problem is that the output from ChoiceField is a string, not an integer. if you use get_type_display() immediately after saving the form, you will probably get this value, but when you try to extract the value from the DB, you will get integer instead of the string (because you save the type as a whole field), here you cannot get the value using get_type_display.
Looking at this, I see that you should use TypedChoiceField to ensure that the output of cleaned_data is always an integer or a string.
first change the IntergerField field to Char or SmallIntergetField .
Hope this helps.
the code
type = models.SmallIntegerField(choices=TYPE_CHOICES)
in forms.py
type = TypedChoiceField(coerce=int, required=False, empty_value=0, choices=TYPE_CHOICES)
Another possibility is that you can use MODELFORM and provide widgets for the field.
Forms.py
class abc(forms.Modelform) class Meta: model = FOO widgets = { 'type': forms.TypedChoiceField(coerce=int, required=False, empty_value=0, choices=TYPE_CHOICES),
source share