Using get_FOO_display ()

I want to show a human-readable name for the selected type, but I keep getting the stored value.

TYPE_CHOICES = ( ('0', 'Basic'), ('1', 'Full'), ('2', 'Intermediate'), ) class ServiceType(models.Model): type = models.IntegerField(max_length=1, choices=TYPE_CHOICES) amount = models.DecimalField(max_digits=10, decimal_places=2) def __unicode__(self): return '%s' % (self.get_type_display()) 
+4
source share
5 answers

Beginners error, I changed the values ​​of the tuple from ('0', 'Basic) to (0,' Basic '), and it worked. I did not realize that I was saving the char value as an integer value.

Thanks for your help.

0
source

You seem to have your own answer, but as another link, I just would like to point out James Bennett’s thoughts on this subject: Choose the right path correctly

I think this is a pretty convenient way to do something and remove the "magic number". It is worth reading the IMO, even if you go for another option.

From his article (cited in case of its disappearance):

 class Entry(models.Model): LIVE_STATUS = 1 DRAFT_STATUS = 2 HIDDEN_STATUS = 3 STATUS_CHOICES = ( (LIVE_STATUS, 'Live'), (DRAFT_STATUS, 'Draft'), (HIDDEN_STATUS, 'Hidden'), ) # ...some other fields here... status = models.IntegerField(choices=STATUS_CHOICES, default=LIVE_STATUS) 

Now we can just import the input model and query like this:

 live_entries = Entry.objects.filter(status=Entry.LIVE_STATUS) draft_entries = Entry.objects.filter(status=Entry.DRAFT_STATUS) 
+4
source

You probably want to use ChoiceField instead of IntegerField in your model. It looks like you see an input tag with type = text in your admin, but want to select the select tag. The default widget associated with IntegerField is TextInput, which explains what you see.

Another option is to write your own administrator and explicitly call that you want the type to be ChoiceField in admin. Something like that:

 class ServiceTypeAdmin(admin.ModelAdmin): # ... type = fields.ChoiceField(choices=TYPE_CHOICES) admin.site.register(ServiceType, ServiceTypeAdmin) 

I would personally start by switching IntegerField to ChoiceField. Way less work.

+2
source

I had the same problem and couldn’t understand why it works, but if you change the field type to CharField, get_type_display should work fine.

 TYPE_CHOICES = ( ('B', 'Basic'), ('F', 'Full'), ('I', 'Intermediate'), ) class ServiceType(models.Model): type = models.CharField(max_length=1, choices=TYPE_CHOICES) amount = models.DecimalField(max_digits=10, decimal_places=2) 
+1
source

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), 
0
source

Source: https://habr.com/ru/post/1300646/


All Articles