Ticket #10271: admin-formset-inheritance.2.diff

File admin-formset-inheritance.2.diff, 11.8 KB (added by Alex Gaynor, 16 years ago)
  • django/contrib/admin/options.py

    diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
    index ed41c7f..7c7d73d 100644
    a b class ModelAdmin(BaseModelAdmin):  
    522522            else:
    523523                form_validated = False
    524524                new_object = self.model()
    525             for FormSet in self.get_formsets(request):
     525            for idx, FormSet in enumerate(self.get_formsets(request)):
    526526                formset = FormSet(data=request.POST, files=request.FILES,
    527527                                  instance=new_object,
    528                                   save_as_new=request.POST.has_key("_saveasnew"))
     528                                  save_as_new=request.POST.has_key("_saveasnew"),
     529                                  prefix="form-%s" % idx)
    529530                formsets.append(formset)
    530531            if all_valid(formsets) and form_validated:
    531532                self.save_model(request, new_object, form, change=False)
    class ModelAdmin(BaseModelAdmin):  
    547548                if isinstance(f, models.ManyToManyField):
    548549                    initial[k] = initial[k].split(",")
    549550            form = ModelForm(initial=initial)
    550             for FormSet in self.get_formsets(request):
    551                 formset = FormSet(instance=self.model())
     551            for idx, FormSet in enumerate(self.get_formsets(request)):
     552                formset = FormSet(instance=self.model(), prefix="form-%s" % idx)
    552553                formsets.append(formset)
    553554       
    554555        adminForm = helpers.AdminForm(form, list(self.get_fieldsets(request)), self.prepopulated_fields)
    class ModelAdmin(BaseModelAdmin):  
    608609            else:
    609610                form_validated = False
    610611                new_object = obj
    611             for FormSet in self.get_formsets(request, new_object):
     612            for idx, FormSet in enumerate(self.get_formsets(request, new_object)):
    612613                formset = FormSet(request.POST, request.FILES,
    613                                   instance=new_object)
     614                                  instance=new_object, prefix="form-%s" % idx)
    614615                formsets.append(formset)
    615616           
    616617            if all_valid(formsets) and form_validated:
    class ModelAdmin(BaseModelAdmin):  
    625626       
    626627        else:
    627628            form = ModelForm(instance=obj)
    628             for FormSet in self.get_formsets(request, obj):
    629                 formset = FormSet(instance=obj)
     629            for idx, FormSet in enumerate(self.get_formsets(request, obj)):
     630                formset = FormSet(instance=obj, prefix="form-%s" % idx)
    630631                formsets.append(formset)
    631632       
    632633        adminForm = helpers.AdminForm(form, self.get_fieldsets(request, obj), self.prepopulated_fields)
  • django/contrib/contenttypes/generic.py

    diff --git a/django/contrib/contenttypes/generic.py b/django/contrib/contenttypes/generic.py
    index c294f74..f581684 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, prefix=None,
     295                    save_as_new=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/regressiontests/admin_views/tests.py

    diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py
    index 39daf11..a511647 100644
    a b class AdminViewBasicTest(TestCase):  
    6363        post_data = {
    6464            "name": u"Another Section",
    6565            # inline data
    66             "article_set-TOTAL_FORMS": u"3",
    67             "article_set-INITIAL_FORMS": u"0",
     66            "form-0-TOTAL_FORMS": u"3",
     67            "form-0-INITIAL_FORMS": u"0",
    6868        }
    6969        response = self.client.post('/test_admin/%s/admin_views/section/add/' % self.urlbit, post_data)
    7070        self.failUnlessEqual(response.status_code, 302) # redirect somewhere
    class AdminViewBasicTest(TestCase):  
    7676        post_data = {
    7777            "name": u"Test section",
    7878            # inline data
    79             "article_set-TOTAL_FORMS": u"6",
    80             "article_set-INITIAL_FORMS": u"3",
    81             "article_set-0-id": u"1",
     79            "form-0-TOTAL_FORMS": u"6",
     80            "form-0-INITIAL_FORMS": u"3",
     81            "form-0-0-id": u"1",
    8282            # there is no title in database, give one here or formset
    8383            # will fail.
    84             "article_set-0-title": u"Norske bostaver æøå skaper problemer",
    85             "article_set-0-content": u"<p>Middle content</p>",
    86             "article_set-0-date_0": u"2008-03-18",
    87             "article_set-0-date_1": u"11:54:58",
    88             "article_set-1-id": u"2",
    89             "article_set-1-title": u"Need a title.",
    90             "article_set-1-content": u"<p>Oldest content</p>",
    91             "article_set-1-date_0": u"2000-03-18",
    92             "article_set-1-date_1": u"11:54:58",
    93             "article_set-2-id": u"3",
    94             "article_set-2-title": u"Need a title.",
    95             "article_set-2-content": u"<p>Newest content</p>",
    96             "article_set-2-date_0": u"2009-03-18",
    97             "article_set-2-date_1": u"11:54:58",
    98             "article_set-3-id": u"",
    99             "article_set-3-title": u"",
    100             "article_set-3-content": u"",
    101             "article_set-3-date_0": u"",
    102             "article_set-3-date_1": u"",
    103             "article_set-4-id": u"",
    104             "article_set-4-title": u"",
    105             "article_set-4-content": u"",
    106             "article_set-4-date_0": u"",
    107             "article_set-4-date_1": u"",
    108             "article_set-5-id": u"",
    109             "article_set-5-title": u"",
    110             "article_set-5-content": u"",
    111             "article_set-5-date_0": u"",
    112             "article_set-5-date_1": u"",
     84            "form-0-0-title": u"Norske bostaver æøå skaper problemer",
     85            "form-0-0-content": u"<p>Middle content</p>",
     86            "form-0-0-date_0": u"2008-03-18",
     87            "form-0-0-date_1": u"11:54:58",
     88            "form-0-1-id": u"2",
     89            "form-0-1-title": u"Need a title.",
     90            "form-0-1-content": u"<p>Oldest content</p>",
     91            "form-0-1-date_0": u"2000-03-18",
     92            "form-0-1-date_1": u"11:54:58",
     93            "form-0-2-id": u"3",
     94            "form-0-2-title": u"Need a title.",
     95            "form-0-2-content": u"<p>Newest content</p>",
     96            "form-0-2-date_0": u"2009-03-18",
     97            "form-0-2-date_1": u"11:54:58",
     98            "form-0-3-id": u"",
     99            "form-0-3-title": u"",
     100            "form-0-3-content": u"",
     101            "form-0-3-date_0": u"",
     102            "form-0-3-date_1": u"",
     103            "form-0-4-id": u"",
     104            "form-0-4-title": u"",
     105            "form-0-4-content": u"",
     106            "form-0-4-date_0": u"",
     107            "form-0-4-date_1": u"",
     108            "form-0-5-id": u"",
     109            "form-0-5-title": u"",
     110            "form-0-5-content": u"",
     111            "form-0-5-date_0": u"",
     112            "form-0-5-date_1": u"",
    113113        }
    114114        response = self.client.post('/test_admin/%s/admin_views/section/1/' % self.urlbit, post_data)
    115115        self.failUnlessEqual(response.status_code, 302) # redirect somewhere
    class AdminViewUnicodeTest(TestCase):  
    687687        post_data = {
    688688            "name": u"Test lærdommer",
    689689            # inline data
    690             "chapter_set-TOTAL_FORMS": u"6",
    691             "chapter_set-INITIAL_FORMS": u"3",
    692             "chapter_set-0-id": u"1",
    693             "chapter_set-0-title": u"Norske bostaver æøå skaper problemer",
    694             "chapter_set-0-content": u"<p>Svært frustrerende med UnicodeDecodeError</p>",
    695             "chapter_set-1-id": u"2",
    696             "chapter_set-1-title": u"Kjærlighet.",
    697             "chapter_set-1-content": u"<p>La kjærligheten til de lidende seire.</p>",
    698             "chapter_set-2-id": u"3",
    699             "chapter_set-2-title": u"Need a title.",
    700             "chapter_set-2-content": u"<p>Newest content</p>",
    701             "chapter_set-3-id": u"",
    702             "chapter_set-3-title": u"",
    703             "chapter_set-3-content": u"",
    704             "chapter_set-4-id": u"",
    705             "chapter_set-4-title": u"",
    706             "chapter_set-4-content": u"",
    707             "chapter_set-5-id": u"",
    708             "chapter_set-5-title": u"",
    709             "chapter_set-5-content": u"",
     690            "form-0-TOTAL_FORMS": u"6",
     691            "form-0-INITIAL_FORMS": u"3",
     692            "form-0-0-id": u"1",
     693            "form-0-0-title": u"Norske bostaver æøå skaper problemer",
     694            "form-0-0-content": u"<p>Svært frustrerende med UnicodeDecodeError</p>",
     695            "form-0-1-id": u"2",
     696            "form-0-1-title": u"Kjærlighet.",
     697            "form-0-1-content": u"<p>La kjærligheten til de lidende seire.</p>",
     698            "form-0-2-id": u"3",
     699            "form-0-2-title": u"Need a title.",
     700            "form-0-2-content": u"<p>Newest content</p>",
     701            "form-0-3-id": u"",
     702            "form-0-3-title": u"",
     703            "form-0-3-content": u"",
     704            "form-0-4-id": u"",
     705            "form-0-4-title": u"",
     706            "form-0-4-content": u"",
     707            "form-0-5-id": u"",
     708            "form-0-5-title": u"",
     709            "form-0-5-content": u"",
    710710        }
    711711
    712712        response = self.client.post('/test_admin/admin/admin_views/book/1/', post_data)
  • tests/regressiontests/generic_inline_admin/tests.py

    diff --git a/tests/regressiontests/generic_inline_admin/tests.py b/tests/regressiontests/generic_inline_admin/tests.py
    index a3ea5fc..3276199 100644
    a b class GenericAdminViewTest(TestCase):  
    5151        post_data = {
    5252            "name": u"This Week in Django",
    5353            # inline data
    54             "generic_inline_admin-media-content_type-object_id-TOTAL_FORMS": u"1",
    55             "generic_inline_admin-media-content_type-object_id-INITIAL_FORMS": u"0",
     54            "form-0-TOTAL_FORMS": u"1",
     55            "form-0-INITIAL_FORMS": u"0",
    5656        }
    5757        response = self.client.post('/generic_inline_admin/admin/generic_inline_admin/episode/add/', post_data)
    5858        self.failUnlessEqual(response.status_code, 302) # redirect somewhere
    class GenericAdminViewTest(TestCase):  
    6464        post_data = {
    6565            "name": u"This Week in Django",
    6666            # inline data
    67             "generic_inline_admin-media-content_type-object_id-TOTAL_FORMS": u"2",
    68             "generic_inline_admin-media-content_type-object_id-INITIAL_FORMS": u"1",
    69             "generic_inline_admin-media-content_type-object_id-0-id": u"%d" % self.media_pk,
    70             "generic_inline_admin-media-content_type-object_id-0-url": u"http://example.com/podcast.mp3",
    71             "generic_inline_admin-media-content_type-object_id-1-id": u"",
    72             "generic_inline_admin-media-content_type-object_id-1-url": u"",
     67            "form-0-TOTAL_FORMS": u"2",
     68            "form-0-INITIAL_FORMS": u"1",
     69            "form-0-0-id": u"%d" % self.media_pk,
     70            "form-0-0-url": u"http://example.com/podcast.mp3",
     71            "form-0-1-id": u"",
     72            "form-0-1-url": u"",
    7373        }
    7474        url = '/generic_inline_admin/admin/generic_inline_admin/episode/%d/' % self.episode_pk
    7575        response = self.client.post(url, post_data)
Back to Top