diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
index 12490c8..4188a95 100644
a
|
b
|
class ModelAdmin(BaseModelAdmin):
|
420 | 420 | for inline in self.inline_instances: |
421 | 421 | yield inline.get_formset(request, obj) |
422 | 422 | |
| 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 | |
423 | 448 | def log_addition(self, request, object): |
424 | 449 | """ |
425 | 450 | Log that an object has been successfully added. |
… |
… |
class ModelAdmin(BaseModelAdmin):
|
777 | 802 | |
778 | 803 | ModelForm = self.get_form(request) |
779 | 804 | formsets = [] |
| 805 | inlines = [] |
780 | 806 | if request.method == 'POST': |
781 | 807 | form = ModelForm(request.POST, request.FILES) |
782 | 808 | if form.is_valid(): |
… |
… |
class ModelAdmin(BaseModelAdmin):
|
785 | 811 | else: |
786 | 812 | form_validated = False |
787 | 813 | 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 | |
799 | 818 | if all_valid(formsets) and form_validated: |
800 | 819 | self.save_model(request, new_object, form, change=False) |
801 | 820 | form.save_m2m() |
… |
… |
class ModelAdmin(BaseModelAdmin):
|
816 | 835 | if isinstance(f, models.ManyToManyField): |
817 | 836 | initial[k] = initial[k].split(",") |
818 | 837 | 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) |
829 | 840 | |
830 | 841 | adminForm = helpers.AdminForm(form, list(self.get_fieldsets(request)), |
831 | 842 | self.prepopulated_fields, self.get_readonly_fields(request), |
… |
… |
class ModelAdmin(BaseModelAdmin):
|
833 | 844 | media = self.media + adminForm.media |
834 | 845 | |
835 | 846 | inline_admin_formsets = [] |
836 | | for inline, formset in zip(self.inline_instances, formsets): |
| 847 | for formset, inline in zip(formsets, inlines): |
837 | 848 | fieldsets = list(inline.get_fieldsets(request)) |
838 | 849 | readonly = list(inline.get_readonly_fields(request)) |
839 | 850 | inline_admin_formset = helpers.InlineAdminFormSet(inline, formset, |
… |
… |
class ModelAdmin(BaseModelAdmin):
|
841 | 852 | inline_admin_formsets.append(inline_admin_formset) |
842 | 853 | media = media + inline_admin_formset.media |
843 | 854 | |
| 855 | |
844 | 856 | context = { |
845 | 857 | 'title': _('Add %s') % force_unicode(opts.verbose_name), |
846 | 858 | 'adminform': adminForm, |
… |
… |
class ModelAdmin(BaseModelAdmin):
|
875 | 887 | |
876 | 888 | ModelForm = self.get_form(request, obj) |
877 | 889 | formsets = [] |
| 890 | inlines = [] |
878 | 891 | if request.method == 'POST': |
879 | 892 | form = ModelForm(request.POST, request.FILES, instance=obj) |
880 | 893 | if form.is_valid(): |
… |
… |
class ModelAdmin(BaseModelAdmin):
|
883 | 896 | else: |
884 | 897 | form_validated = False |
885 | 898 | 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) |
898 | 901 | |
899 | 902 | if all_valid(formsets) and form_validated: |
900 | 903 | self.save_model(request, new_object, form, change=True) |
… |
… |
class ModelAdmin(BaseModelAdmin):
|
908 | 911 | |
909 | 912 | else: |
910 | 913 | 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) |
920 | 915 | |
921 | 916 | adminForm = helpers.AdminForm(form, self.get_fieldsets(request, obj), |
922 | 917 | self.prepopulated_fields, self.get_readonly_fields(request, obj), |
… |
… |
class ModelAdmin(BaseModelAdmin):
|
924 | 919 | media = self.media + adminForm.media |
925 | 920 | |
926 | 921 | inline_admin_formsets = [] |
927 | | for inline, formset in zip(self.inline_instances, formsets): |
| 922 | for formset, inline in zip(formsets, inlines): |
928 | 923 | fieldsets = list(inline.get_fieldsets(request, obj)) |
929 | 924 | readonly = list(inline.get_readonly_fields(request, obj)) |
930 | 925 | inline_admin_formset = helpers.InlineAdminFormSet(inline, formset, |
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::
|
900 | 900 | return qs |
901 | 901 | return qs.filter(author=request.user) |
902 | 902 | |
| 903 | .. method:: ModelAdmin.get_formsets(self, request, obj=None) |
| 904 | |
| 905 | The ``get_formsets`` method on ``ModelAdmin`` returns the formsets for the |
| 906 | registered 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 | |
| 915 | The ``get_formset_instances`` method on ``ModelAdmin`` returns the formsets |
| 916 | and inlines for the requested page. |
| 917 | |
| 918 | |
903 | 919 | Other methods |
904 | 920 | ~~~~~~~~~~~~~ |
905 | 921 | |