Edwin199
Edwin199

Reputation: 1

How to display option-names instead of option-values?

I poked around with get_FOO_display to no avail, here's what I've got (I trimmed some stuff to make it easier to read, like a custom def save:

class Profile(models.Model):
LEVEL_CHOICES = (
(1, 'Primary'),
(2, 'Secondary'),
(3, 'Tertiary'),
)

level = models.IntegerField( verbose_name = 'Level', blank=True, null=True, choices=LEVEL_CHOICES)
piplevel = models.ForeignKey(AccessLevel, related_name='profiles')
vanity = models.ForeignKey(AccessLevel, related_name='vanity')



class ProfileForm(forms.ModelForm):
    class Meta:
        model = Profile


 # HTML CODE from Template below
    <tbody>
        {% for field in user_form %}
        <tr>
                            <th>{{ field.label_tag }}</th>
                            <td class="updatable">
                                <div class="field">
                                    {{ field }}
                                </div>
                                {% if field.errors %}{{ field.errors }}{% endif %}
                            </td>
        </tr>
        {% endfor %}

        {% for field in profile_form %}
        <tr>
                            <th>{{ field.label_tag }}</th>
                            <td class="updatable">
                                <div class="field">
                                    {{ field }}
                                </div>
                                {% if field.errors %}{{ field.errors }}{% endif %}
                            </td>
                        </tr>
        {% endfor %}
    </tbody>

The output I get:

Level 1
Piplevel 15
Vanity 20

The output I want:

Level Primary
Piplevel Diamond
Vanity Ambassador

I tried replacing the {{ field }} and for loop with {{field.get.level.display}} and {{ field.get_piplevel_display }} but it they just returned blanks. When these display, they're a drop-down menu and DO have all the named-options available - that is, when I click on the "15" I do get an option list i.e.:

Piplevel 15 click (current selected is 15)(option value="1">Regular(/option>(option value="2">Platinum(/option>etc. (not html code)

Am I missing some easy function in Django for this?

Edit: Here's the HTML output I GET currently.

 <tr class="even">
<th>
<label for="id_piplevel">Piplevel</label>
</th>
<td class="updatable" title="Click to modify">
<div class="field" style="display: none;">
<select id="id_piplevel" name="piplevel">
<option value="">---------</option>
<option value="1">Regular</option>
<option value="2">Platinum</option>
...
<option selected="selected" value="7">Emerald</option>
</select>
</div>
<div>7</div>
</td>
</tr>

Here's the HTML output I WANT, ideally:

 <tr class="even">
<th>
<label for="id_piplevel">Piplevel</label>
</th>
<td class="updatable" title="Click to modify">
<div class="field" style="display: none;">
<select id="id_piplevel" name="piplevel">
<option value="">---------</option>
<option value="1">Regular</option>
<option value="2">Platinum</option>
...
<option selected="selected" value="7">Emerald</option>
</select>
</div>
<div>Emerald</div>
</td>
</tr>

And, if I replace {{ field }} with {{field.get_piplevel_display}} or {{field.choice.get_piplevel_display}}, I get this as output:

<tr class="even">
<th>
<label for="id_piplevel">Piplevel</label>
</th>
<td class="updatable">
<div class="field"> </div>
</td>
</tr>

It seems like this is something everyone would want, and so I expected there to be some argument in Django that just... fixes this, but I can't find it. Sorry for making a simple question seem so complicated - this is supposed to be a simple question, isn't it?

Upvotes: 0

Views: 2524

Answers (2)

Chris Pratt
Chris Pratt

Reputation: 239290

For every field that has choices, Django automatically adds a get_FOO_display() method on the model (docs). All this method does is take the already stored value for the field, looks it up in the provided choices for the field and returns the "display" text for that value.

This method is not available on the field or the form. You must reference an existing object or the instance of the form (e.g. form.instance).

Again, I'm not sure what you're trying to accomplish, because the <select>s are already populated such that they display the text and not the underlying value. Even if you're editing an existing object, the <select> will still show the display text for the currently set value.

Upvotes: 4

Ted
Ted

Reputation: 12318

I don't really see what you're trying to do, but:

{{field.get.level.display}}

should be something like

{{field.choices.get_level_display}}
# or
{{field.get_level_display}}

Upvotes: 3

Related Questions