Code

Ticket #15907: 15907_at_16452.diff

File 15907_at_16452.diff, 3.2 KB (added by prestontimmons, 3 years ago)

Patch with fix moved to generic_inlineformset_factory

Line 
1diff --git a/django/contrib/contenttypes/generic.py b/django/contrib/contenttypes/generic.py
2index 389692d..274a6d6 100644
3--- a/django/contrib/contenttypes/generic.py
4+++ b/django/contrib/contenttypes/generic.py
5@@ -382,6 +382,9 @@ def generic_inlineformset_factory(model, form=ModelForm,
6         exclude.extend([ct_field.name, fk_field.name])
7     else:
8         exclude = [ct_field.name, fk_field.name]
9+        if hasattr(form, 'Meta') and hasattr(form.Meta, 'exclude'):
10+            exclude.extend(list(form.Meta.exclude))
11+
12     FormSet = modelformset_factory(model, form=form,
13                                    formfield_callback=formfield_callback,
14                                    formset=formset,
15diff --git a/tests/regressiontests/generic_inline_admin/models.py b/tests/regressiontests/generic_inline_admin/models.py
16index fb6363e..205124f 100644
17--- a/tests/regressiontests/generic_inline_admin/models.py
18+++ b/tests/regressiontests/generic_inline_admin/models.py
19@@ -2,6 +2,7 @@ from django.db import models
20 from django.contrib import admin
21 from django.contrib.contenttypes import generic
22 from django.contrib.contenttypes.models import ContentType
23+from django.forms.models import ModelForm
24 
25 class Episode(models.Model):
26     name = models.CharField(max_length=100)
27@@ -73,6 +74,29 @@ class MediaExcludeInline(generic.GenericTabularInline):
28 admin.site.register(EpisodeExclude, inlines=[MediaExcludeInline])
29 
30 #
31+# Generic inline with modelform and exclude
32+#
33+
34+class Sitcom(Episode):
35+    pass
36+
37+class MediaForm(ModelForm):
38+    class Meta:
39+        model = Media
40+        exclude = ('url',)
41+
42+class MediaInline(generic.GenericTabularInline):
43+    form = MediaForm
44+    model = Media
45+    extra = 1
46+
47+class SitcomAdmin(admin.ModelAdmin):
48+    inlines = (MediaInline, )
49+
50+admin.site.register(Sitcom, SitcomAdmin)
51+
52+
53+#
54 # Generic inline with unique_together
55 #
56 
57diff --git a/tests/regressiontests/generic_inline_admin/tests.py b/tests/regressiontests/generic_inline_admin/tests.py
58index 00badc9..081474d 100644
59--- a/tests/regressiontests/generic_inline_admin/tests.py
60+++ b/tests/regressiontests/generic_inline_admin/tests.py
61@@ -7,7 +7,8 @@ from django.test import TestCase
62 
63 # local test models
64 from models import (Episode, EpisodeExtra, EpisodeMaxNum, EpisodeExclude,
65-    Media, MediaInline, EpisodePermanent, MediaPermanentInline, Category)
66+    Media, MediaInline, EpisodePermanent, MediaPermanentInline, Category,
67+    Sitcom)
68 
69 
70 class GenericAdminViewTest(TestCase):
71@@ -182,6 +183,15 @@ class GenericInlineAdminParametersTest(TestCase):
72         formset = response.context['inline_admin_formsets'][0].formset
73         self.assertFalse('url' in formset.forms[0], 'The formset has excluded "url" field.')
74 
75+    def testModelFormExcludeParam(self):
76+        """
77+        Generic inline formsets should respect exclude on model forms.
78+        """
79+        e = self._create_object(Sitcom)
80+        response = self.client.get('/generic_inline_admin/admin/generic_inline_admin/sitcom/%s/' % e.pk)
81+        formset = response.context['inline_admin_formsets'][0].formset
82+        self.assertFalse('url' in formset.forms[0].fields, 'The formset has excluded "url" field.')
83+
84 class GenericInlineAdminWithUniqueTogetherTest(TestCase):
85     fixtures = ['users.xml']
86