Ticket #10281: 10281-generic-inline-formset-prefix.diff

File 10281-generic-inline-formset-prefix.diff, 2.3 KB (added by Antti Kaihola, 16 years ago)

Patch adds the prefix= kwarg to generic inline formsets. Includes a new test.

  • django/contrib/contenttypes/generic.py

    diff --git a/django/contrib/contenttypes/generic.py b/django/contrib/contenttypes/generic.py
    index c294f74..d4023f3 100644
    a b class BaseGenericInlineFormSet(BaseModelFormSet):  
    291291    ct_field_name = "content_type"
    292292    ct_fk_field_name = "object_id"
    293293
    294     def __init__(self, data=None, files=None, instance=None, save_as_new=None):
     294    def __init__(self, data=None, files=None, instance=None,
     295                 save_as_new=None, prefix=None):
    295296        opts = self.model._meta
    296297        self.instance = instance
    297298        self.rel_name = '-'.join((
    class BaseGenericInlineFormSet(BaseModelFormSet):  
    300301        ))
    301302        super(BaseGenericInlineFormSet, self).__init__(
    302303            queryset=self.get_queryset(), data=data, files=files,
    303             prefix=self.rel_name
     304            prefix=prefix or self.rel_name
    304305        )
    305306
    306307    def get_queryset(self):
  • tests/modeltests/generic_relations/models.py

    diff --git a/tests/modeltests/generic_relations/models.py b/tests/modeltests/generic_relations/models.py
    index db5ae47..2c8bbfa 100644
    a b __test__ = {'API_TESTS':"""  
    233233<p><label for="id_generic_relations-taggeditem-content_type-object_id-1-tag">Tag:</label> <input id="id_generic_relations-taggeditem-content_type-object_id-1-tag" type="text" name="generic_relations-taggeditem-content_type-object_id-1-tag" maxlength="50" /></p>
    234234<p><label for="id_generic_relations-taggeditem-content_type-object_id-1-DELETE">Delete:</label> <input type="checkbox" name="generic_relations-taggeditem-content_type-object_id-1-DELETE" id="id_generic_relations-taggeditem-content_type-object_id-1-DELETE" /><input type="hidden" name="generic_relations-taggeditem-content_type-object_id-1-id" id="id_generic_relations-taggeditem-content_type-object_id-1-id" /></p>
    235235
     236>>> formset = GenericFormSet(instance=lion, prefix='x')
     237>>> for form in formset.forms:
     238...     print form.as_p()
     239    <p><label for="id_x-0-tag">Tag:</label> <input id="id_x-0-tag" type="text" name="x-0-tag" maxlength="50" /></p>
     240    <p><label for="id_x-0-DELETE">Delete:</label> <input type="checkbox" name="x-0-DELETE" id="id_x-0-DELETE" /><input type="hidden" name="x-0-id" id="id_x-0-id" /></p>
     241
    236242"""}
Back to Top