diff --git a/django/forms/models.py b/django/forms/models.py index 88e1ff93f2..56e19e7145 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -401,13 +401,14 @@ def modelformset_factory(model, form=ModelForm, formfield_callback=lambda f: f.f class BaseInlineFormset(BaseModelFormSet): """A formset for child objects related to a parent.""" - def __init__(self, data=None, files=None, instance=None, save_as_new=False): + def __init__(self, data=None, files=None, instance=None, + save_as_new=False, prefix=None): from django.db.models.fields.related import RelatedObject self.instance = instance self.save_as_new = save_as_new # is there a better way to get the object descriptor? self.rel_name = RelatedObject(self.fk.rel.to, self.model, self.fk).get_accessor_name() - super(BaseInlineFormset, self).__init__(data, files, prefix=self.rel_name) + super(BaseInlineFormset, self).__init__(data, files, prefix=prefix or self.rel_name) def _construct_forms(self): if self.save_as_new: diff --git a/tests/modeltests/model_formsets/models.py b/tests/modeltests/model_formsets/models.py index 4a89bc33d7..81355400cf 100644 --- a/tests/modeltests/model_formsets/models.py +++ b/tests/modeltests/model_formsets/models.py @@ -313,4 +313,13 @@ True >>> [book for book in formset.save() if book.author.pk == new_author.pk] [, ] +Test using a custom prefix on an inline formset. + +>>> formset = AuthorBooksFormSet(prefix="test") +>>> for form in formset.forms: +... print form.as_p() +

+

+ + """}