2
votes

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?

1
I've created an issue for this github.com/AndrewIngram/django-extra-views/issues/35. I'll try and get it sorted quickly.Andrew Ingram
thnx! that'd be great @AndrewIngram I'll put a watch on django-extra-views in the meantime ;)darren
Just realised that the inlines are already given the request. You should be able to override get_extra_form_kwargs on your inline to pass the user object to each form instance.Andrew Ingram
@AndrewIngram I updated the question. If you wouldn't mind taking a sneek peek. Greatly appreciated.darren
there was a bug in 0.6.0, try 0.6.1. Also in your example you have get_formset_kwargs rather than get_extra_form_kwargs, is this a typo? Your form will break too, I'll put the 'correct' code as an answer.Andrew Ingram

1 Answers

3
votes

There was an issue with django-extra-views==0.6.0 that meant get_extra_form_kwargs wasn't being called for inlines, it should be fixed in 0.6.1.

This should work:

class EventMemberInline(InlineFormSet):
    model = EventMember
    extra = 1
    form_class = EventMemberForm

    def get_extra_form_kwargs(self):
        kwargs = super(EventMemberInline, self).get_extra_form_kwargs()
        kwargs.update({
            'first_name': self.request.user.first_name,
            'last_name': self.request.user.last_name
        })
        return kwargs

...

class EventMemberForm(ModelForm):

    class Meta:
        model = EventMember

    def __init__(self, *args, **kwargs):
        initial_first_name = kwargs.pop('first_name')
        initial_last_name = kwargs.pop('last_name')

        super(EventMemberForm, self).__init__(*args, **kwargs)

        self.fields['first_name'].initial = initial_first_name
        self.fields['last_name'].initial = initial_last_name