Blank option in required ChoiceField

前端 未结 4 1124
醉梦人生
醉梦人生 2021-02-20 09:35

I want my ChoiceField in ModelForm to have a blank option (------) but it\'s required.

I need to have blank option to prevent user from accidentally skipping the field t

4条回答
  •  旧时难觅i
    2021-02-20 09:42

    in argument add null = True

    like this

    gender = models.CharField(max_length=1, null = True)
    

    http://docs.djangoproject.com/en/dev/ref/models/fields/


    for your comment

    THEME_CHOICES = (
        ('--', '-----'),
        ('DR', 'Domain_registery'),
    )
        theme = models.CharField(max_length=2, choices=THEME_CHOICES)
    

提交回复
热议问题