django form creation on init
Posted
by John
on Stack Overflow
See other posts from Stack Overflow
or by John
Published on 2010-04-08T12:47:27Z
Indexed on
2010/04/08
12:53 UTC
Read the original article
Hit count: 234
django
|django-forms
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
© Stack Overflow or respective owner