Display "Choose Option" label in Django ModelForm ChoiceField -
i have django modelform choicefield , display text "choose option" in dropdown when user visits page. ("choose option" wouldn't actual "value" rather "label")
any thoughts how can add code below?
i appreciate time.
class exerciseform(forms.form): def __init__(self, *args, **kwargs): super(exerciseform, self).__init__(*args, **kwargs) exercises = exercise.objects.all().order_by('name') exercise_choices = [(exercise.youtube_id, exercise.name) exercise in exercises] self.fields['exercise'] = forms.choicefield(choices=exercise_choices)
just use label argument, like:
self.fields['exercise'] = forms.choicefield(choices=exercise_choices, label="choose option")
or if present inside dropdown without being actual choice insert first option:
exercises = exercise.objects.all().order_by('name') exercise_choices = [(exercise.youtube_id, exercise.name) exercise in exercises] exercise_choices.insert(0, (u"", u"choose option")) self.fields['exercise'] = forms.choicefield(choices=exercise_choices)
Comments
Post a Comment