Customize/remove Django select box blank option

后端 未结 15 814
佛祖请我去吃肉
佛祖请我去吃肉 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条回答
  •  独厮守ぢ
    2020-11-30 19:13

    For a ForeignKey field, setting the default value to '' on the model will remove the blank option.

    verb = models.ForeignKey(Verb, on_delete=models.CASCADE, default='')
    

    For other fields like CharField you could set the default to None, but this does not work for ForeignKey fields in Django 1.11.

提交回复
热议问题