Ticket #14580: admin_get_formset_instances2.diff

File admin_get_formset_instances2.diff, 8.2 KB (added by apollo13, 5 years ago)
  • django/contrib/admin/options.py

    diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
    index 12490c8..4188a95 100644
    a b class ModelAdmin(BaseModelAdmin): 
    420420        for inline in self.inline_instances:
    421421            yield inline.get_formset(request, obj)
    422422
     423    def get_formset_instances(self, request, obj, change=True):
     424        prefixes = {}
     425
     426        formsets = []
     427        inlines = []
     428
     429        for FormSet, inline in zip(self.get_formsets(request), self.inline_instances):
     430            prefix = FormSet.get_default_prefix()
     431            prefixes[prefix] = prefixes.get(prefix, 0) + 1
     432            if prefixes[prefix] != 1:
     433                prefix = "%s-%s" % (prefix, prefixes[prefix])
     434            if request.method == 'POST':
     435                formset = FormSet(data=request.POST, files=request.FILES,
     436                                  instance=obj,
     437                                  save_as_new=request.POST.has_key("_saveasnew"),
     438                                  prefix=prefix, queryset=inline.queryset(request))
     439            else:
     440                formset = FormSet(instance=obj, prefix=prefix,
     441                                  queryset=inline.queryset(request))
     442
     443            formsets.append(formset)
     444            inlines.append(inline)
     445
     446        return formsets, inlines
     447
    423448    def log_addition(self, request, object):
    424449        """
    425450        Log that an object has been successfully added.
    class ModelAdmin(BaseModelAdmin): 
    777802
    778803        ModelForm = self.get_form(request)
    779804        formsets = []
     805        inlines = []
    780806        if request.method == 'POST':
    781807            form = ModelForm(request.POST, request.FILES)
    782808            if form.is_valid():
    class ModelAdmin(BaseModelAdmin): 
    785811            else:
    786812                form_validated = False
    787813                new_object = self.model()
    788             prefixes = {}
    789             for FormSet, inline in zip(self.get_formsets(request), self.inline_instances):
    790                 prefix = FormSet.get_default_prefix()
    791                 prefixes[prefix] = prefixes.get(prefix, 0) + 1
    792                 if prefixes[prefix] != 1:
    793                     prefix = "%s-%s" % (prefix, prefixes[prefix])
    794                 formset = FormSet(data=request.POST, files=request.FILES,
    795                                   instance=new_object,
    796                                   save_as_new=request.POST.has_key("_saveasnew"),
    797                                   prefix=prefix, queryset=inline.queryset(request))
    798                 formsets.append(formset)
     814
     815            formsets, inlines = self.get_formset_instances(request, new_object,
     816                                                           False)
     817
    799818            if all_valid(formsets) and form_validated:
    800819                self.save_model(request, new_object, form, change=False)
    801820                form.save_m2m()
    class ModelAdmin(BaseModelAdmin): 
    816835                if isinstance(f, models.ManyToManyField):
    817836                    initial[k] = initial[k].split(",")
    818837            form = ModelForm(initial=initial)
    819             prefixes = {}
    820             for FormSet, inline in zip(self.get_formsets(request),
    821                                        self.inline_instances):
    822                 prefix = FormSet.get_default_prefix()
    823                 prefixes[prefix] = prefixes.get(prefix, 0) + 1
    824                 if prefixes[prefix] != 1:
    825                     prefix = "%s-%s" % (prefix, prefixes[prefix])
    826                 formset = FormSet(instance=self.model(), prefix=prefix,
    827                                   queryset=inline.queryset(request))
    828                 formsets.append(formset)
     838            formsets, inlines = self.get_formset_instances(request, self.model(),
     839                                                           False)
    829840
    830841        adminForm = helpers.AdminForm(form, list(self.get_fieldsets(request)),
    831842            self.prepopulated_fields, self.get_readonly_fields(request),
    class ModelAdmin(BaseModelAdmin): 
    833844        media = self.media + adminForm.media
    834845
    835846        inline_admin_formsets = []
    836         for inline, formset in zip(self.inline_instances, formsets):
     847        for formset, inline in zip(formsets, inlines):
    837848            fieldsets = list(inline.get_fieldsets(request))
    838849            readonly = list(inline.get_readonly_fields(request))
    839850            inline_admin_formset = helpers.InlineAdminFormSet(inline, formset,
    class ModelAdmin(BaseModelAdmin): 
    841852            inline_admin_formsets.append(inline_admin_formset)
    842853            media = media + inline_admin_formset.media
    843854
     855
    844856        context = {
    845857            'title': _('Add %s') % force_unicode(opts.verbose_name),
    846858            'adminform': adminForm,
    class ModelAdmin(BaseModelAdmin): 
    875887
    876888        ModelForm = self.get_form(request, obj)
    877889        formsets = []
     890        inlines = []
    878891        if request.method == 'POST':
    879892            form = ModelForm(request.POST, request.FILES, instance=obj)
    880893            if form.is_valid():
    class ModelAdmin(BaseModelAdmin): 
    883896            else:
    884897                form_validated = False
    885898                new_object = obj
    886             prefixes = {}
    887             for FormSet, inline in zip(self.get_formsets(request, new_object),
    888                                        self.inline_instances):
    889                 prefix = FormSet.get_default_prefix()
    890                 prefixes[prefix] = prefixes.get(prefix, 0) + 1
    891                 if prefixes[prefix] != 1:
    892                     prefix = "%s-%s" % (prefix, prefixes[prefix])
    893                 formset = FormSet(request.POST, request.FILES,
    894                                   instance=new_object, prefix=prefix,
    895                                   queryset=inline.queryset(request))
    896 
    897                 formsets.append(formset)
     899            formsets, inlines = self.get_formset_instances(request, new_object,
     900                                                           True)
    898901
    899902            if all_valid(formsets) and form_validated:
    900903                self.save_model(request, new_object, form, change=True)
    class ModelAdmin(BaseModelAdmin): 
    908911
    909912        else:
    910913            form = ModelForm(instance=obj)
    911             prefixes = {}
    912             for FormSet, inline in zip(self.get_formsets(request, obj), self.inline_instances):
    913                 prefix = FormSet.get_default_prefix()
    914                 prefixes[prefix] = prefixes.get(prefix, 0) + 1
    915                 if prefixes[prefix] != 1:
    916                     prefix = "%s-%s" % (prefix, prefixes[prefix])
    917                 formset = FormSet(instance=obj, prefix=prefix,
    918                                   queryset=inline.queryset(request))
    919                 formsets.append(formset)
     914            formsets, inlines = self.get_formset_instances(request, obj, True)
    920915
    921916        adminForm = helpers.AdminForm(form, self.get_fieldsets(request, obj),
    922917            self.prepopulated_fields, self.get_readonly_fields(request, obj),
    class ModelAdmin(BaseModelAdmin): 
    924919        media = self.media + adminForm.media
    925920
    926921        inline_admin_formsets = []
    927         for inline, formset in zip(self.inline_instances, formsets):
     922        for formset, inline in zip(formsets, inlines):
    928923            fieldsets = list(inline.get_fieldsets(request, obj))
    929924            readonly = list(inline.get_readonly_fields(request, obj))
    930925            inline_admin_formset = helpers.InlineAdminFormSet(inline, formset,
  • docs/ref/contrib/admin/index.txt

    diff --git a/docs/ref/contrib/admin/index.txt b/docs/ref/contrib/admin/index.txt
    index 0550576..6dfeada 100644
    a b to show objects owned by the logged-in user:: 
    900900                return qs
    901901            return qs.filter(author=request.user)
    902902
     903.. method:: ModelAdmin.get_formsets(self, request, obj=None)
     904
     905The ``get_formsets`` method on ``ModelAdmin`` returns the formsets for the
     906registered inline classes.
     907
     908.. warning::
     909
     910    Don't change the order of the formsets or skip some of them. Use
     911    ``get_formsets_instances`` if you have to do something like that.
     912
     913.. method:: ModelAdmin.get_formset_instances(self, request, obj, change=True)
     914
     915The ``get_formset_instances`` method on ``ModelAdmin`` returns the formsets
     916and inlines for the requested page.
     917
     918
    903919Other methods
    904920~~~~~~~~~~~~~
    905921
Back to Top