Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added the ability to customize the prefix value on an inline formset.…

… Fixes #7899. AThanks for the tip Peter of the Norse.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@8060 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 3912403550e31ddb3a002789dd2fe3379a4e006e 1 parent 681e7bb
Brian Rosner brosner authored
5 django/forms/models.py
View
@@ -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:
9 tests/modeltests/model_formsets/models.py
View
@@ -313,4 +313,13 @@ def __unicode__(self):
>>> [book for book in formset.save() if book.author.pk == new_author.pk]
[<Book: Les Fleurs du Mal>, <Book: Le Spleen de Paris>]
+Test using a custom prefix on an inline formset.
+
+>>> formset = AuthorBooksFormSet(prefix="test")
+>>> for form in formset.forms:
+... print form.as_p()
+<p><label for="id_test-0-title">Title:</label> <input id="id_test-0-title" type="text" name="test-0-title" maxlength="100" /><input type="hidden" name="test-0-id" id="id_test-0-id" /></p>
+<p><label for="id_test-1-title">Title:</label> <input id="id_test-1-title" type="text" name="test-1-title" maxlength="100" /><input type="hidden" name="test-1-id" id="id_test-1-id" /></p>
+
+
"""}
Please sign in to comment.
Something went wrong with that request. Please try again.