Empty Label ChoiceField Django

后端 未结 7 928
猫巷女王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:41

    Here's the solution that I used:

    from myapp.models import COLORS
    
    COLORS_EMPTY = [('','---------')] + COLORS
    
    class ColorBrowseForm(forms.Form):
        color = forms.ChoiceField(choices=COLORS_EMPTY, required=False, widget=forms.Select(attrs={'onchange': 'this.form.submit();'}))
    
    0 讨论(0)
提交回复
热议问题