Opened 9 years ago

Closed 9 years ago

#24888 closed Uncategorized (worksforme)

A charfield with options shows with short tags in stead of verbose name in a modelform

Reported by: Wim Feijen Owned by: nobody
Component: Forms Version: 1.8
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Suppose you have a charfield in your model which has several choices defined. When you create a form from this model, the form shows the short tags as options (which are used for database storage) in stead of the verbose name (get_xxx_display) which should be used by default for displaying, in my opinion.

Example:

FURNISHING = (
    ("yes", _("Furnished")),
    ("no", _("Unfurnished")),
    ("bare", _("Bare")),
)

class Room(TimeStamped):
    ...
    furnishing = models.CharField(verbose_name=_("furnishing"), max_length=10, choices=FURNISHING, default='no')
    ...

FIELDS = [..., 'furnishing', ...]

class RoomForm(forms.ModelForm):
    class Meta:
        model = Room
        fields = FIELDS
        widgets = {
            'furnishing': forms.RadioSelect(),
        }

(A workaround is to use the verbose names as char tags in the database. However, this does not work in a multilingual environment.)

Change History (1)

comment:1 by Tim Graham, 9 years ago

Resolution: worksforme
Status: newclosed

Here's the HTML I get with the code your provided (using return render(request, 'form.html', {'form': RoomForm()}) as the view and {{ form }} in the template):

<tr><th><label for="id_furnishing_0">Furnishing:</label></th><td><ul id="id_furnishing"><li><label for="id_furnishing_0"><input id="id_furnishing_0" name="furnishing" type="radio" value="yes" /> Furnished</label></li>
<li><label for="id_furnishing_1"><input checked="checked" id="id_furnishing_1" name="furnishing" type="radio" value="no" /> Unfurnished</label></li>
<li><label for="id_furnishing_2"><input id="id_furnishing_2" name="furnishing" type="radio" value="bare" /> Bare</label></li></ul></td></tr>

This seems to be the expected result. Please reopen if you can provide more specific steps to reproduce.

Note: See TracTickets for help on using tickets.
Back to Top