1

I have a ModelForm that has a widget of type Select

 class MyModelForm(ModelForm):
    ...
    widgets = {
        'my_field': Select(
            choices=[('1', 'Choice 1')]
        ),
    }

In my view I'm retrieving an already stored model and passing the form as my_form to the template:

<div>{{ my_form.my_field.value }}</div>

As expected, this outputs:

 1

How can I get the text 'Choice 1' instead?

damores
  • 2,251
  • 2
  • 18
  • 31

1 Answers1

1

While not an exact duplicate, arogachev's duplicate suggestion did help me realize I should be setting the choices attribute in the Model's fields and not the ModelForm's widgets:

class MyModel(Model):
    my_field = models.CharField(max_length=50, choices=[
        ('1', 'Choice 1')
    ])

This way, I can do this:

<div>{{ my_form.instance.get_my_field_display }}</div>

Which outputs

Choice 1

Thanks!

Community
  • 1
  • 1
damores
  • 2,251
  • 2
  • 18
  • 31