Customize/remove Django select box blank option

后端 未结 15 827
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-30 18:22

I\'m using Django 1.0.2. I\'ve written a ModelForm backed by a Model. This model has a ForeignKey where blank=False. When Django generates HTML for this form it creates a

15条回答
  •  Happy的楠姐
    2020-11-30 18:56

    Since Django 1.7, you can customize the label for the blank value by adding a value to your choices list in your model field definition. From the documentation on configuring field choices:

    Unless blank=False is set on the field along with a default then a label containing "---------" will be rendered with the select box. To override this behavior, add a tuple to choices containing None; e.g. (None, 'Your String For Display'). Alternatively, you can use an empty string instead of None where this makes sense - such as on a CharField.

    I checked the documentation for different versions of Django and found that this was added in Django 1.7.

提交回复
热议问题