I have a form in my Django that looks something like this:
class PersonnelForm(forms.Form):
"""
Form for creating a new personnel.
"""
username = forms.RegexField(
required=True, max_length=30, label=_("Name")
)
is_manager = forms.BooleanField(
required=True, label=_("Is Manager")
)
I use this form in two places in my site. One one of the places, I'd like to display the form and all of its fields except the is_manager
field but I would like to set the default value of this field to True
. In the other place, I'd like to display the form and all of its fields including the is_manager
field and I would like it to have a default value of False.
How can I accomplish this? Seems to be a trivial thing but I can't figure it out.
Thanks.
You could use the form's
__init__
method to hide (or delete) the field, i.e.If the functionalities are different, you can use inherit one form from the other and display sutiably(i.e exclude fields).
Also, Form's init method can be made to take arguments, and this can be used to initialize the form's field values.