Skip to content
Browse files

Changing the get_formset method to return a tuple that includes the f…

…ormsets inline, fixing ticket #20702
  • Loading branch information...
1 parent a1831a1 commit ea7c7dc856fd773d74214099f646f8c4e2c7f2f3 @tim-schilling committed Aug 12, 2013
View
16 django/contrib/admin/options.py
@@ -504,7 +504,7 @@ def get_changelist_formset(self, request, **kwargs):
def get_formsets(self, request, obj=None):
for inline in self.get_inline_instances(request):
- yield inline.get_formset(request, obj)
+ yield inline.get_formset(request, obj), inline
def get_paginator(self, request, queryset, per_page, orphans=0, allow_empty_first_page=True):
return self.paginator(queryset, per_page, orphans, allow_empty_first_page)
@@ -941,7 +941,7 @@ def add_view(self, request, form_url='', extra_context=None):
form_validated = False
new_object = self.model()
prefixes = {}
- for FormSet, inline in zip(self.get_formsets(request), inline_instances):
+ for FormSet, inline in self.get_formsets(request):
prefix = FormSet.get_default_prefix()
prefixes[prefix] = prefixes.get(prefix, 0) + 1
if prefixes[prefix] != 1 or not prefix:
@@ -969,7 +969,7 @@ def add_view(self, request, form_url='', extra_context=None):
initial[k] = initial[k].split(",")
form = ModelForm(initial=initial)
prefixes = {}
- for FormSet, inline in zip(self.get_formsets(request), inline_instances):
+ for FormSet, inline in self.get_formsets(request):
prefix = FormSet.get_default_prefix()
prefixes[prefix] = prefixes.get(prefix, 0) + 1
if prefixes[prefix] != 1 or not prefix:
@@ -1029,7 +1029,7 @@ def change_view(self, request, object_id, form_url='', extra_context=None):
ModelForm = self.get_form(request, obj)
formsets = []
- inline_instances = self.get_inline_instances(request)
+ used_inline_instances = []
if request.method == 'POST':
form = ModelForm(request.POST, request.FILES, instance=obj)
if form.is_valid():
@@ -1039,7 +1039,7 @@ def change_view(self, request, object_id, form_url='', extra_context=None):
form_validated = False
new_object = obj
prefixes = {}
- for FormSet, inline in zip(self.get_formsets(request, new_object), inline_instances):
+ for FormSet, inline in self.get_formsets(request, new_object):
prefix = FormSet.get_default_prefix()
prefixes[prefix] = prefixes.get(prefix, 0) + 1
if prefixes[prefix] != 1 or not prefix:
@@ -1049,6 +1049,7 @@ def change_view(self, request, object_id, form_url='', extra_context=None):
queryset=inline.queryset(request))
formsets.append(formset)
+ used_inline_instances.append(inline)
if all_valid(formsets) and form_validated:
self.save_model(request, new_object, form, True)
@@ -1060,14 +1061,15 @@ def change_view(self, request, object_id, form_url='', extra_context=None):
else:
form = ModelForm(instance=obj)
prefixes = {}
- for FormSet, inline in zip(self.get_formsets(request, obj), inline_instances):
+ for FormSet, inline in self.get_formsets(request, obj):
prefix = FormSet.get_default_prefix()
prefixes[prefix] = prefixes.get(prefix, 0) + 1
if prefixes[prefix] != 1 or not prefix:
prefix = "%s-%s" % (prefix, prefixes[prefix])
formset = FormSet(instance=obj, prefix=prefix,
queryset=inline.queryset(request))
formsets.append(formset)
+ used_inline_instances.append(inline)
adminForm = helpers.AdminForm(form, self.get_fieldsets(request, obj),
self.get_prepopulated_fields(request, obj),
@@ -1076,7 +1078,7 @@ def change_view(self, request, object_id, form_url='', extra_context=None):
media = self.media + adminForm.media
inline_admin_formsets = []
- for inline, formset in zip(inline_instances, formsets):
+ for inline, formset in zip(used_inline_instances, formsets):
fieldsets = list(inline.get_fieldsets(request, obj))
readonly = list(inline.get_readonly_fields(request, obj))
prepopulated = dict(inline.get_prepopulated_fields(request, obj))
View
6 tests/regressiontests/generic_inline_admin/tests.py
@@ -274,7 +274,7 @@ class EpisodeAdmin(admin.ModelAdmin):
ma = EpisodeAdmin(Episode, self.site)
self.assertEqual(
- list(ma.get_formsets(request))[0]().forms[0].fields.keys(),
+ list(ma.get_formsets(request))[0][0]().forms[0].fields.keys(),
['keywords', 'id', 'DELETE'])
def test_custom_form_meta_exclude(self):
@@ -304,7 +304,7 @@ class EpisodeAdmin(admin.ModelAdmin):
ma = EpisodeAdmin(Episode, self.site)
self.assertEqual(
- list(ma.get_formsets(request))[0]().forms[0].fields.keys(),
+ list(ma.get_formsets(request))[0][0]().forms[0].fields.keys(),
['url', 'keywords', 'id', 'DELETE'])
# Then, only with `ModelForm` -----------------
@@ -320,5 +320,5 @@ class EpisodeAdmin(admin.ModelAdmin):
ma = EpisodeAdmin(Episode, self.site)
self.assertEqual(
- list(ma.get_formsets(request))[0]().forms[0].fields.keys(),
+ list(ma.get_formsets(request))[0][0]().forms[0].fields.keys(),
['description', 'keywords', 'id', 'DELETE'])
View
2 tests/regressiontests/modeladmin/admin.py
@@ -30,7 +30,7 @@ def get_formsets(self, request, obj=None):
continue
# Do not return the concert inline
if isinstance(inline, SongInline):
- yield inline.get_formset(request, obj)
+ yield inline.get_formset(request, obj), inline
site.register(Band, BandAdmin)
View
8 tests/regressiontests/modeladmin/tests.py
@@ -180,7 +180,7 @@ class BandAdmin(ModelAdmin):
ma = BandAdmin(Band, self.site)
self.assertEqual(
- list(ma.get_formsets(request))[0]().forms[0].fields.keys(),
+ list(ma.get_formsets(request))[0][0]().forms[0].fields.keys(),
['main_band', 'opening_band', 'id', 'DELETE',])
def test_custom_form_meta_exclude(self):
@@ -226,7 +226,7 @@ class BandAdmin(ModelAdmin):
ma = BandAdmin(Band, self.site)
self.assertEqual(
- list(ma.get_formsets(request))[0]().forms[0].fields.keys(),
+ list(ma.get_formsets(request))[0][0]().forms[0].fields.keys(),
['main_band', 'opening_band', 'day', 'id', 'DELETE',])
def test_custom_form_validation(self):
@@ -304,7 +304,7 @@ class BandAdmin(ModelAdmin):
ma = BandAdmin(Band, self.site)
self.assertEqual(
- list(ma.get_formsets(request))[0]().forms[0].fields.keys(),
+ list(ma.get_formsets(request))[0][0]().forms[0].fields.keys(),
['main_band', 'day', 'transport', 'id', 'DELETE',])
def test_queryset_override(self):
@@ -499,7 +499,7 @@ class BandAdmin(ModelAdmin):
ma = BandAdmin(Band, self.site)
self.assertEqual(
- list(ma.get_formsets(request))[0]().forms[0].fields.keys(),
+ list(ma.get_formsets(request))[0][0]().forms[0].fields.keys(),
['extra', 'transport', 'id', 'DELETE', 'main_band'])
def test_override_inlines_with_get_formsets(self):

0 comments on commit ea7c7dc

Please sign in to comment.
Something went wrong with that request. Please try again.