Code

Ticket #13145: custom-empty-form-prefix.2.diff

File custom-empty-form-prefix.2.diff, 5.1 KB (added by Alex, 4 years ago)
Line 
1diff --git a/django/contrib/admin/media/js/inlines.js b/django/contrib/admin/media/js/inlines.js
2index 4e397b6..1f5933f 100644
3--- a/django/contrib/admin/media/js/inlines.js
4+++ b/django/contrib/admin/media/js/inlines.js
5@@ -56,7 +56,7 @@
6                                var template = $("#" + options.prefix + "-empty");
7                                var row = template.clone(true).get(0);
8                                $(row).removeClass(options.emptyCssClass).removeAttr("id").insertBefore($(template));
9-                               $(row).html($(row).html().replace(/__prefix__/g, nextIndex));
10+                               $(row).html($(row).html().replace(new RegExp(options.empty_form_prefix, "g"), nextIndex));
11                                $(row).addClass(options.formCssClass).attr("id", options.prefix + nextIndex);
12                                if ($(row).is("TR")) {
13                                        // If the forms are laid out in table rows, insert
14@@ -118,6 +118,7 @@
15        /* Setup plugin defaults */
16        $.fn.formset.defaults = {
17                prefix: "form",                                 // The form prefix for your django formset
18+               empty_form_prefix: "__prefix__",// The magic prefix bit on the empty form.
19                addText: "add another",                 // Text for the add link
20                deleteText: "remove",                   // Text for the delete link
21                addCssClass: "add-row",                 // CSS class applied to the add link
22diff --git a/django/contrib/admin/templates/admin/edit_inline/stacked.html b/django/contrib/admin/templates/admin/edit_inline/stacked.html
23index fb112a0..81879d0 100644
24--- a/django/contrib/admin/templates/admin/edit_inline/stacked.html
25+++ b/django/contrib/admin/templates/admin/edit_inline/stacked.html
26@@ -50,6 +50,7 @@
27         }
28         $(rows).formset({
29             prefix: "{{ inline_admin_formset.formset.prefix }}",
30+            empty_form_prefix: "{{ inline_admin_formset.formset.empty_form_prefix }}",
31             addText: "{% blocktrans with inline_admin_formset.opts.verbose_name|title as verbose_name %}Add another {{ verbose_name }}{% endblocktrans %}",
32             formCssClass: "dynamic-{{ inline_admin_formset.formset.prefix }}",
33             deleteCssClass: "inline-deletelink",
34diff --git a/django/contrib/admin/templates/admin/edit_inline/tabular.html b/django/contrib/admin/templates/admin/edit_inline/tabular.html
35index dc9552f..d56944a 100644
36--- a/django/contrib/admin/templates/admin/edit_inline/tabular.html
37+++ b/django/contrib/admin/templates/admin/edit_inline/tabular.html
38@@ -96,6 +96,7 @@
39         }
40         $(rows).formset({
41             prefix: "{{ inline_admin_formset.formset.prefix }}",
42+            empty_form_prefix: "{{ inline_admin_formset.formset.empty_form_prefix }}",
43             addText: "{% blocktrans with inline_admin_formset.opts.verbose_name|title as verbose_name %}Add another {{ verbose_name }}{% endblocktrans %}",
44             formCssClass: "dynamic-{{ inline_admin_formset.formset.prefix }}",
45             deleteCssClass: "inline-deletelink",
46diff --git a/django/forms/formsets.py b/django/forms/formsets.py
47index ec14f81..03aa7ba 100644
48--- a/django/forms/formsets.py
49+++ b/django/forms/formsets.py
50@@ -32,6 +32,8 @@ class BaseFormSet(StrAndUnicode):
51     """
52     A collection of instances of the same Form class.
53     """
54+    empty_form_prefix = '__prefix__'
55+
56     def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
57                  initial=None, error_class=ErrorList):
58         self.is_bound = data is not None or files is not None
59@@ -125,7 +127,7 @@ class BaseFormSet(StrAndUnicode):
60     def _get_empty_form(self, **kwargs):
61         defaults = {
62             'auto_id': self.auto_id,
63-            'prefix': self.add_prefix('__prefix__'),
64+            'prefix': self.add_prefix(self.empty_form_prefix),
65             'empty_permitted': True,
66         }
67         if self.data or self.files:
68diff --git a/tests/regressiontests/forms/formsets.py b/tests/regressiontests/forms/formsets.py
69index 3eecb7f..6c9bf01 100644
70--- a/tests/regressiontests/forms/formsets.py
71+++ b/tests/regressiontests/forms/formsets.py
72@@ -1,4 +1,19 @@
73 # -*- coding: utf-8 -*-
74+
75+from django import forms
76+from django.forms.formsets import formset_factory
77+from django.test import TestCase
78+
79+class EmptyFormTestCase(TestCase):
80+    def test_custom_empty_form_prefix(self):
81+        class BasicForm(forms.Form):
82+            value = forms.CharField()
83+       
84+        FormSet = formset_factory(BasicForm)
85+        FormSet.empty_form_prefix = "__pony__"
86+        self.assertEqual(str(FormSet().empty_form), """<tr><th><label for="id_form-__pony__-value">Value:</label></th><td><input type="text" name="form-__pony__-value" id="id_form-__pony__-value" /></td></tr>""")
87+
88+
89 tests = """
90 # Basic FormSet creation and usage ############################################
91 
92diff --git a/tests/regressiontests/forms/tests.py b/tests/regressiontests/forms/tests.py
93index 8757e79..e50a10f 100644
94--- a/tests/regressiontests/forms/tests.py
95+++ b/tests/regressiontests/forms/tests.py
96@@ -34,7 +34,7 @@ from localflavor.za import tests as localflavor_za_tests
97 from regressions import tests as regression_tests
98 from util import tests as util_tests
99 from widgets import tests as widgets_tests
100-from formsets import tests as formset_tests
101+from formsets import tests as formset_tests, EmptyFormTestCase
102 from media import media_tests
103 
104 from fields import FieldsTests