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

File custom-empty-form-prefix.2.diff, 5.1 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..6c9bf01 100644
    a b  
    11# -*- coding: utf-8 -*-
     2
     3from django import forms
     4from django.forms.formsets import formset_factory
     5from django.test import TestCase
     6
     7class EmptyFormTestCase(TestCase):
     8    def test_custom_empty_form_prefix(self):
     9        class BasicForm(forms.Form):
     10            value = forms.CharField()
     11       
     12        FormSet = formset_factory(BasicForm)
     13        FormSet.empty_form_prefix = "__pony__"
     14        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>""")
     15
     16
    217tests = """
    318# Basic FormSet creation and usage ############################################
    419
  • 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