django form creation on init
- by John
Hi,
How can I add a field in the form init function? e.g. in the code below I want to add a profile field.
class StaffForm(forms.ModelForm):
def __init__(self, user, *args, **kwargs):
if user.pk == 1:
self.fields['profile'] = forms.CharField(max_length=200)
super(StaffForm, self).__init__(*args, **kwargs)
class Meta:
model = Staff
I know I can add it just below the class StaffForm.... line but I want this to be dynamic depending on what user is passed in so can't do it this way.
Thanks