2

based upon this answer regarding formset_factory, I tried to do the same thing for modelformset_factory:

from django.utils.functional import curry
from functools wraps

AccountMemberFormSetBase = modelformset_factory(AccountMember,
                                                form=wraps(AccountMemberLimitedModelForm)(curry(AccountMemberLimitedModelForm, affiliate="test")),
                                                extra=2)

This throws the following error:

function() argument 1 must be code, not str
Exception Location: ../django/forms/models.py in modelform_factory, line 528

Any idea whats wrong here?

3
  • Could you show the full traceback? Also, what django version are you using? Commented Jul 2, 2014 at 14:17
  • Django 1.6 - will post the full traceback soon Commented Jul 2, 2014 at 14:20
  • Here is the full traceback: pastebin.com/htt0ANFi Commented Jul 2, 2014 at 15:20

2 Answers 2

1

I found myself in the same situation today.

See my comment in Django Passing Custom Form Parameters to Formset for details and a workaround (I hope a link to StackOverflow itself is acceptable).

Sign up to request clarification or add additional context in comments.

Comments

0
class AccountMemberLimitedModelForm(forms.Form):
...     def __init__(self, *args, **kwargs):
...         affiliate = kwargs.pop('affiliate')
...         super(AccountMemberLimitedModelForm, self).__init__(*args, **kwargs)



AccountMemberFormSetBase = formset_factory(AccountMemberLimitedModelForm)
    formset = AccountMemberFormSetBase(form_kwargs={'affiliate': "test"})

3 Comments

It would be nice if you posted more than just some code. Perhaps you could explain why that code works?
with kwargs.pop('affiliate') I add external variable to the form. It must be done before calling super(AccountMemberLimitedModelForm, self).__init__(*args, **kwargs) to take effect. And when I pass the variable to form from formset by using form_kwargs dictionary

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.