Sometimes, we want to add a Python Django required field in model form.
In this article, we’ll look at how to add a Python Django required field in model form.
How to add a Python Django required field in model form?
To add a Python Django required field in model form, we set the required
property of the field to True
.
For instance, we write
class MyForm(CircuitForm):
def __init__(self, *args, **kwargs):
super(CircuitForm, self).__init__(*args, **kwargs)
for key in self.fields:
self.fields[key].required = True
to loop through the fields in the form with a for loop.
In the loop, we set the field to required with
self.fields[key].required = True
Now all fields are set to be required.
Conclusion
To add a Python Django required field in model form, we set the required
property of the field to True
.