Ticket #14580: admin_get_formset_instances.diff

File admin_get_formset_instances.diff, 8.7 KB (added by Florian Apolloner, 14 years ago)
  • django/contrib/admin/options.py

    diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
    index 12490c8..9b00fcd 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        inline_admin_formsets = []
     427
     428        for FormSet, inline in zip(self.get_formsets(request), self.inline_instances):
     429            prefix = FormSet.get_default_prefix()
     430            prefixes[prefix] = prefixes.get(prefix, 0) + 1
     431            if prefixes[prefix] != 1:
     432                prefix = "%s-%s" % (prefix, prefixes[prefix])
     433            if request.method == 'POST':
     434                formset = FormSet(data=request.POST, files=request.FILES,
     435                                  instance=obj,
     436                                  save_as_new=request.POST.has_key("_saveasnew"),
     437                                  prefix=prefix, queryset=inline.queryset(request))
     438            else:
     439                formset = FormSet(instance=obj, prefix=prefix,
     440                                  queryset=inline.queryset(request))
     441
     442            fieldsets = list(inline.get_fieldsets(request))
     443            readonly = list(inline.get_readonly_fields(request))
     444            inline_admin_formset = helpers.InlineAdminFormSet(inline, formset,
     445                fieldsets, readonly, model_admin=self)
     446            inline_admin_formsets.append(inline_admin_formset)
     447
     448        return inline_admin_formsets
     449
    423450    def log_addition(self, request, object):
    424451        """
    425452        Log that an object has been successfully added.
    class ModelAdmin(BaseModelAdmin):  
    776803            raise PermissionDenied
    777804
    778805        ModelForm = self.get_form(request)
     806        inline_admin_formsets = []
    779807        formsets = []
    780808        if request.method == 'POST':
    781809            form = ModelForm(request.POST, request.FILES)
    class ModelAdmin(BaseModelAdmin):  
    785813            else:
    786814                form_validated = False
    787815                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)
     816            inline_admin_formsets = self.get_formset_instances(request, new_object, False)
     817            formsets = map(lambda x: x.formset, inline_admin_formsets)
     818
    799819            if all_valid(formsets) and form_validated:
    800820                self.save_model(request, new_object, form, change=False)
    801821                form.save_m2m()
    class ModelAdmin(BaseModelAdmin):  
    816836                if isinstance(f, models.ManyToManyField):
    817837                    initial[k] = initial[k].split(",")
    818838            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)
     839            inline_admin_formsets = self.get_formset_instances(request, self.model(), False)
     840            formsets = map(lambda x: x.formset, inline_admin_formsets)
    829841
    830842        adminForm = helpers.AdminForm(form, list(self.get_fieldsets(request)),
    831843            self.prepopulated_fields, self.get_readonly_fields(request),
    832844            model_admin=self)
    833845        media = self.media + adminForm.media
    834846
    835         inline_admin_formsets = []
    836         for inline, formset in zip(self.inline_instances, formsets):
    837             fieldsets = list(inline.get_fieldsets(request))
    838             readonly = list(inline.get_readonly_fields(request))
    839             inline_admin_formset = helpers.InlineAdminFormSet(inline, formset,
    840                 fieldsets, readonly, model_admin=self)
    841             inline_admin_formsets.append(inline_admin_formset)
     847        for inline_admin_formset in inline_admin_formsets:
    842848            media = media + inline_admin_formset.media
    843849
    844850        context = {
    class ModelAdmin(BaseModelAdmin):  
    874880            return self.add_view(request, form_url='../add/')
    875881
    876882        ModelForm = self.get_form(request, obj)
     883        inline_admin_formsets = []
    877884        formsets = []
    878885        if request.method == 'POST':
    879886            form = ModelForm(request.POST, request.FILES, instance=obj)
    class ModelAdmin(BaseModelAdmin):  
    883890            else:
    884891                form_validated = False
    885892                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)
     893            inline_admin_formsets = self.get_formset_instances(request, new_object, True)
     894            formsets = map(lambda x: x.formset, inline_admin_formsets)
    898895
    899896            if all_valid(formsets) and form_validated:
    900897                self.save_model(request, new_object, form, change=True)
    class ModelAdmin(BaseModelAdmin):  
    908905
    909906        else:
    910907            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)
     908            inline_admin_formsets = self.get_formset_instances(request, obj, True)
     909            formsets = map(lambda x: x.formset, inline_admin_formsets)
    920910
    921911        adminForm = helpers.AdminForm(form, self.get_fieldsets(request, obj),
    922912            self.prepopulated_fields, self.get_readonly_fields(request, obj),
    923913            model_admin=self)
    924914        media = self.media + adminForm.media
    925915
    926         inline_admin_formsets = []
    927         for inline, formset in zip(self.inline_instances, formsets):
    928             fieldsets = list(inline.get_fieldsets(request, obj))
    929             readonly = list(inline.get_readonly_fields(request, obj))
    930             inline_admin_formset = helpers.InlineAdminFormSet(inline, formset,
    931                 fieldsets, readonly, model_admin=self)
    932             inline_admin_formsets.append(inline_admin_formset)
     916        for inline_admin_formset in inline_admin_formsets:
    933917            media = media + inline_admin_formset.media
    934918
    935919        context = {
  • docs/ref/contrib/admin/index.txt

    diff --git a/docs/ref/contrib/admin/index.txt b/docs/ref/contrib/admin/index.txt
    index 0550576..009b016 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
     916for the requested page.
     917
     918
    903919Other methods
    904920~~~~~~~~~~~~~
    905921
Back to Top