Reputation: 950
I have a Django model in which I have a models.CharField
named level
I then create a ModelForm from this model, and I want the level to be a select
from a ChoiceField.
So I create a new widget in the ModelForm.
The only problem is that then it seems like I can't redefine the labels for the level
. It only appear as "level" in my form.
from django import forms
from events.models import Event
class EventForm(forms.ModelForm):
level = forms.ChoiceField(choices=[("RT","RT"), ("RT+","RT+"), ("RI", "RI"), ("RI+", "RI+"), ("RS", "RS"), ("RS+", "RS+"), ("RE", "RE")])
class Meta:
model = Event
labels = {
"title": "Titre de l'évènement",
"level": "Niveau de la randonnée", # HERE: wont rename the label
"map_url": "Lien Google Map de l'itinéraire",
"date": "Date et heure de l'activité (au format DD/MM/YYYY HH:MM)"
}
exclude = ("slug",)
Upvotes: 0
Views: 148
Reputation: 47354
labels
attribute works only for fields generated automatically. Quote from the docs:
Fields defined declaratively are left as-is, therefore any customizations made to Meta attributes such as widgets, labels, help_texts, or error_messages are ignored; these only apply to fields that are generated automatically.
You can specify label for explicitly defined fields with label
argument:
level = forms.ChoiceField(label='Some label', choices=[("RT","RT"), ("RT+","RT+"), ("RI", "RI"), ("RI+", "RI+"), ("RS", "RS"), ("RS+", "RS+"), ("RE", "RE")])
Upvotes: 2