Tuesday, March 5, 2013

Re: how to add prefix in generic_inlineformset_factory?

On Tue, Mar 5, 2013 at 9:20 AM, Witold Greń <witold.gren@gmail.com> wrote:
> How to add prefix in generic_inlineformset_factory()? This is posible?
>

I don't understand precisely what you mean, but two of the arguments
to generic_inlineformset_factory() are 'form' and 'formset', allowing
you to specify a different class for the form objects created, and a
different base class for the formset created. This is generally true
for all formset_factory() functions.

This allows you almost unlimited flexibility in controlling how the
forms are generated, how they behave and so forth. Eg, if I wanted the
formset to take an additional argument on construction, and set that
argument on each instance the formset creates/modifies, I could do
something like this:


class TeamBaseFormset(BaseModelFormSet):
def __init__(self, *args, **kwargs):
self.manager = kwargs.pop('manager')
def save(self, commit=True, *args, **kwargs):
instances = super(TeamBaseFormset, self).save(commit=False,
*args, **kwargs)
for team in instances:
team.manager = self.manager
if commit:
team.save()
return instances

TeamFormset = modelformset_factory(Team, formset=TeamBaseFormset)

form = TeamFormset(manager=manager)

Since you have control over the formset class and the form, you can
override or change any behaviour that you want.

Cheers

Tom

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment