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

File custom-empty-form-prefix.3.diff, 6.7 KB (added by Alex Gaynor, 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  
    5656                                var template = $("#" + options.prefix + "-empty");
    5757                                var row = template.clone(true).get(0);
    5858                                $(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));
    6060                                $(row).addClass(options.formCssClass).attr("id", options.prefix + nextIndex);
    6161                                if ($(row).is("TR")) {
    6262                                        // If the forms are laid out in table rows, insert
     
    118118        /* Setup plugin defaults */
    119119        $.fn.formset.defaults = {
    120120                prefix: "form",                                 // The form prefix for your django formset
     121                empty_form_prefix: "__prefix__",// The magic prefix bit on the empty form.
    121122                addText: "add another",                 // Text for the add link
    122123                deleteText: "remove",                   // Text for the delete link
    123124                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  
    5050        }
    5151        $(rows).formset({
    5252            prefix: "{{ inline_admin_formset.formset.prefix }}",
     53            empty_form_prefix: "{{ inline_admin_formset.formset.empty_form_prefix }}",
    5354            addText: "{% blocktrans with inline_admin_formset.opts.verbose_name|title as verbose_name %}Add another {{ verbose_name }}{% endblocktrans %}",
    5455            formCssClass: "dynamic-{{ inline_admin_formset.formset.prefix }}",
    5556            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  
    9696        }
    9797        $(rows).formset({
    9898            prefix: "{{ inline_admin_formset.formset.prefix }}",
     99            empty_form_prefix: "{{ inline_admin_formset.formset.empty_form_prefix }}",
    99100            addText: "{% blocktrans with inline_admin_formset.opts.verbose_name|title as verbose_name %}Add another {{ verbose_name }}{% endblocktrans %}",
    100101            formCssClass: "dynamic-{{ inline_admin_formset.formset.prefix }}",
    101102            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):  
    3232    """
    3333    A collection of instances of the same Form class.
    3434    """
     35    empty_form_prefix = '__prefix__'
     36
    3537    def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
    3638                 initial=None, error_class=ErrorList):
    3739        self.is_bound = data is not None or files is not None
    class BaseFormSet(StrAndUnicode):  
    125127    def _get_empty_form(self, **kwargs):
    126128        defaults = {
    127129            'auto_id': self.auto_id,
    128             'prefix': self.add_prefix('__prefix__'),
     130            'prefix': self.add_prefix(self.empty_form_prefix),
    129131            'empty_permitted': True,
    130132        }
    131133        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  
    11# -*- coding: utf-8 -*-
     2
     3from django import forms
     4from django.forms.formsets import formset_factory
     5from django.forms.models import inlineformset_factory
     6from django.test import TestCase
     7
     8from models import Form, FormPage, Field
     9
     10
     11class 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
    235tests = """
    336# Basic FormSet creation and usage ############################################
    437
  • 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):  
    5656    def __unicode__(self):
    5757        return u'%s' % self.name
    5858
     59class Form(models.Model):
     60    name = models.CharField(max_length=100)
     61
     62
     63class FormPage(models.Model):
     64    form = models.ForeignKey(Form)
     65    page_num = models.IntegerField()
     66
     67
     68class Field(models.Model):
     69    page = models.ForeignKey(FormPage)
     70    label = models.CharField(max_length=100)
     71
     72
    5973class TestTicket12510(TestCase):
    6074    ''' It is not necessary to generate choices for ModelChoiceField (regression test for #12510). '''
    6175    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  
    3434from regressions import tests as regression_tests
    3535from util import tests as util_tests
    3636from widgets import tests as widgets_tests
    37 from formsets import tests as formset_tests
     37from formsets import tests as formset_tests, EmptyFormTestCase
    3838from media import media_tests
    3939
    4040from fields import FieldsTests
Back to Top