Empty Label ChoiceField Django

后端 未结 7 955
猫巷女王i
猫巷女王i 2020-12-23 19:38

How do you make ChoiceField\'s label behave like ModelChoiceField? Is there a way to set an empty_label, or at least show a blank fiel

7条回答
  •  太阳男子
    2020-12-23 20:14

    You can try this (assuming your choices are tuples):

    blank_choice = (('', '---------'),)
    ...
    color = forms.ChoiceField(choices=blank_choice + COLORS)
    year = forms.ChoiceField(choices=blank_choice + YEAR_CHOICES)
    

    Also, I can't tell from your code whether this is a form or a ModelForm, but it it's the latter, no need to redefine the form field here (you can include the choices=COLORS and choices=YEAR_CHOICES directly in the model field.

    Hope this helps.

提交回复
热议问题