Required boolean field?

前端 未结 2 1541
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-19 18:23

I\'ve got a boolean field,

is_operable = BooleanField(widget=RadioSelect(choices=YES_OR_NO, renderer=InlineRadioFieldRenderer), required=False, initial=True)         


        
相关标签:
2条回答
  • 2021-01-19 19:04

    Leave it as required=False and write a custom clean_is_operable method that checks that you've got what you want, else throws a ValidationError or the like

    0 讨论(0)
  • 2021-01-19 19:15

    I would recommend using a TypedChoiceField which coerces the choices YES_OR_NO into a boolean. See the docs: http://docs.djangoproject.com/en/1.2/ref/forms/fields/#django.forms.TypedChoiceField

    0 讨论(0)
提交回复
热议问题