I have a form with an inline form set. What I would like to do is when the user lands on the form, to have his user information (name for example) prepopulated into one of the inlines of the form. To be able to pass the request through to the inlines.
How can I do this?
I'm using django-extra-views 0.6 on Django 1.4
In views.py
I'm using the get_extra_form_kwargs
def to setup my kwargs like so:
class EventMemberInline(InlineFormSet):
model = EventMember
extra = 1
form_class = EventMemberForm
def get_formset_kwargs(self):
formset_kwargs = super(EventMemberInline, self).get_formset_kwargs()
formset_kwargs.update({'first_name':self.request.user.first_name, 'last_name':self.request.user.last_name})
return formset_kwargs
I'm doing that in the attempt that I will be able to bind the fields of my inlines(initial values) to what I pass through the kwargs(in this case just user first_name and last_name)
In forms.py
class EventMemberForm(ModelForm):
class Meta:
model = EventMember
def __init__(self, *args, **kwargs):
self.fields['first_name'].initial = kwargs['first_name']
self.fields['last_name'].initial = kwargs['last_name']
super(EventMemberForm, self).__init__(*args, **kwargs)
But I get this error:
Exception Value:
__init__() got an unexpected keyword argument 'last_name'
Is what I'm trying to do even possible? Can I set the initial value of an inline?
get_extra_form_kwargs
on your inline to pass the user object to each form instance. – Andrew Ingramget_formset_kwargs
rather thanget_extra_form_kwargs
, is this a typo? Your form will break too, I'll put the 'correct' code as an answer. – Andrew Ingram