Ticket #13145: custom-empty-form-prefix.3.diff
File custom-empty-form-prefix.3.diff, 6.7 KB (added by , 15 years ago) |
---|
-
django/contrib/admin/media/js/inlines.js
diff --git a/django/contrib/admin/media/js/inlines.js b/django/contrib/admin/media/js/inlines.js index 4e397b6..1f5933f 100644
a b 56 56 var template = $("#" + options.prefix + "-empty"); 57 57 var row = template.clone(true).get(0); 58 58 $(row).removeClass(options.emptyCssClass).removeAttr("id").insertBefore($(template)); 59 $(row).html($(row).html().replace( /__prefix__/g, nextIndex));59 $(row).html($(row).html().replace(new RegExp(options.empty_form_prefix, "g"), nextIndex)); 60 60 $(row).addClass(options.formCssClass).attr("id", options.prefix + nextIndex); 61 61 if ($(row).is("TR")) { 62 62 // If the forms are laid out in table rows, insert … … 118 118 /* Setup plugin defaults */ 119 119 $.fn.formset.defaults = { 120 120 prefix: "form", // The form prefix for your django formset 121 empty_form_prefix: "__prefix__",// The magic prefix bit on the empty form. 121 122 addText: "add another", // Text for the add link 122 123 deleteText: "remove", // Text for the delete link 123 124 addCssClass: "add-row", // CSS class applied to the add link -
django/contrib/admin/templates/admin/edit_inline/stacked.html
diff --git a/django/contrib/admin/templates/admin/edit_inline/stacked.html b/django/contrib/admin/templates/admin/edit_inline/stacked.html index fb112a0..81879d0 100644
a b 50 50 } 51 51 $(rows).formset({ 52 52 prefix: "{{ inline_admin_formset.formset.prefix }}", 53 empty_form_prefix: "{{ inline_admin_formset.formset.empty_form_prefix }}", 53 54 addText: "{% blocktrans with inline_admin_formset.opts.verbose_name|title as verbose_name %}Add another {{ verbose_name }}{% endblocktrans %}", 54 55 formCssClass: "dynamic-{{ inline_admin_formset.formset.prefix }}", 55 56 deleteCssClass: "inline-deletelink", -
django/contrib/admin/templates/admin/edit_inline/tabular.html
diff --git a/django/contrib/admin/templates/admin/edit_inline/tabular.html b/django/contrib/admin/templates/admin/edit_inline/tabular.html index dc9552f..d56944a 100644
a b 96 96 } 97 97 $(rows).formset({ 98 98 prefix: "{{ inline_admin_formset.formset.prefix }}", 99 empty_form_prefix: "{{ inline_admin_formset.formset.empty_form_prefix }}", 99 100 addText: "{% blocktrans with inline_admin_formset.opts.verbose_name|title as verbose_name %}Add another {{ verbose_name }}{% endblocktrans %}", 100 101 formCssClass: "dynamic-{{ inline_admin_formset.formset.prefix }}", 101 102 deleteCssClass: "inline-deletelink", -
django/forms/formsets.py
diff --git a/django/forms/formsets.py b/django/forms/formsets.py index ec14f81..03aa7ba 100644
a b class BaseFormSet(StrAndUnicode): 32 32 """ 33 33 A collection of instances of the same Form class. 34 34 """ 35 empty_form_prefix = '__prefix__' 36 35 37 def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None, 36 38 initial=None, error_class=ErrorList): 37 39 self.is_bound = data is not None or files is not None … … class BaseFormSet(StrAndUnicode): 125 127 def _get_empty_form(self, **kwargs): 126 128 defaults = { 127 129 'auto_id': self.auto_id, 128 'prefix': self.add_prefix( '__prefix__'),130 'prefix': self.add_prefix(self.empty_form_prefix), 129 131 'empty_permitted': True, 130 132 } 131 133 if self.data or self.files: -
tests/regressiontests/forms/formsets.py
diff --git a/tests/regressiontests/forms/formsets.py b/tests/regressiontests/forms/formsets.py index 3eecb7f..ae54d80 100644
a b 1 1 # -*- coding: utf-8 -*- 2 3 from django import forms 4 from django.forms.formsets import formset_factory 5 from django.forms.models import inlineformset_factory 6 from django.test import TestCase 7 8 from models import Form, FormPage, Field 9 10 11 class EmptyFormTestCase(TestCase): 12 def test_custom_empty_form_prefix(self): 13 class BasicForm(forms.Form): 14 value = forms.CharField() 15 16 FormSet = formset_factory(BasicForm) 17 FormSet.empty_form_prefix = "__pony__" 18 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>""") 19 20 def test_custom_empty_form_prefix_nested_formsets(self): 21 class PageForm(forms.ModelForm): 22 def __init__(self, *args, **kwargs): 23 super(PageForm, self).__init__(*args, **kwargs) 24 self.field_formset = FieldFormSet(self.is_bound and self.data or None, 25 instance=self.instance, prefix=self.prefix) 26 self.field_formset.empty_form_prefix = "__field_prefix__" 27 28 FormPageFormSet = inlineformset_factory(Form, FormPage, form=PageForm) 29 FieldFormSet = inlineformset_factory(FormPage, Field) 30 31 f = FormPageFormSet(instance=Form()) 32 self.assertFalse("__prefix__-__prefix__" in str(f.empty_form.field_formset.empty_form)) 33 34 2 35 tests = """ 3 36 # Basic FormSet creation and usage ############################################ 4 37 -
tests/regressiontests/forms/models.py
diff --git a/tests/regressiontests/forms/models.py b/tests/regressiontests/forms/models.py index 229c505..27e7079 100644
a b class Group(models.Model): 56 56 def __unicode__(self): 57 57 return u'%s' % self.name 58 58 59 class Form(models.Model): 60 name = models.CharField(max_length=100) 61 62 63 class FormPage(models.Model): 64 form = models.ForeignKey(Form) 65 page_num = models.IntegerField() 66 67 68 class Field(models.Model): 69 page = models.ForeignKey(FormPage) 70 label = models.CharField(max_length=100) 71 72 59 73 class TestTicket12510(TestCase): 60 74 ''' It is not necessary to generate choices for ModelChoiceField (regression test for #12510). ''' 61 75 def setUp(self): -
tests/regressiontests/forms/tests.py
diff --git a/tests/regressiontests/forms/tests.py b/tests/regressiontests/forms/tests.py index 8757e79..e50a10f 100644
a b from localflavor.za import tests as localflavor_za_tests 34 34 from regressions import tests as regression_tests 35 35 from util import tests as util_tests 36 36 from widgets import tests as widgets_tests 37 from formsets import tests as formset_tests 37 from formsets import tests as formset_tests, EmptyFormTestCase 38 38 from media import media_tests 39 39 40 40 from fields import FieldsTests