Django required field in model form

前端 未结 6 1519
栀梦
栀梦 2020-11-29 19:44

I have a form where a couple of fields are coming out as required when I don\'t want them too. Here is the form from models.py

class CircuitForm(ModelForm):
         


        
6条回答
  •  庸人自扰
    2020-11-29 20:33

    If you don't want to modify blank setting for your fields inside models (doing so will break normal validation in admin site), you can do the following in your Form class:

    def __init__(self, *args, **kwargs):
        super(CircuitForm, self).__init__(*args, **kwargs)
    
        for key in self.fields:
            self.fields[key].required = False 
    

    The redefined constructor won't harm any functionality.

提交回复
热议问题