Code

Ticket #15907: 15907_patch_at_r16141.diff

File 15907_patch_at_r16141.diff, 3.4 KB (added by prestontimmons, 3 years ago)

Patch combined with tests

Line 
1diff --git a/django/forms/models.py b/django/forms/models.py
2index ecfdc92..4862c8d 100644
3--- a/django/forms/models.py
4+++ b/django/forms/models.py
5@@ -384,6 +384,9 @@ def modelform_factory(model, form=ModelForm, fields=None, exclude=None,
6     # creating needs to inherit from the parent's inner meta.
7     parent = (object,)
8     if hasattr(form, 'Meta'):
9+        if 'exclude' in attrs and hasattr(form.Meta, 'exclude'):
10+            all_exclude = tuple(attrs['exclude']) + tuple(form.Meta.exclude)
11+            attrs['exclude'] = tuple(set(all_exclude))
12         parent = (form.Meta, object)
13     Meta = type('Meta', parent, attrs)
14 
15diff --git a/tests/regressiontests/generic_inline_admin/models.py b/tests/regressiontests/generic_inline_admin/models.py
16index fb6363e..ed412bf 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,28 @@ class MediaExcludeInline(generic.GenericTabularInline):
28 admin.site.register(EpisodeExclude, inlines=[MediaExcludeInline])
29 
30 #
31+# Generic inline with custom modelform and exclude
32+#
33+
34+class EpisodeModelFormExclude(Episode):
35+    pass
36+
37+class ModelFormWithExclude(ModelForm):
38+    class Meta:
39+        model = Media
40+        exclude = ('url',)
41+
42+class MediaWithModelFormExcludeInline(generic.GenericTabularInline):
43+    form = ModelFormWithExclude
44+    model = Media
45+    extra = 1
46+
47+class EpisodeModelFormExcludeAdmin(admin.ModelAdmin):
48+    inlines = (MediaWithModelFormExcludeInline, )
49+
50+admin.site.register(EpisodeModelFormExclude, EpisodeModelFormExcludeAdmin)
51+
52+#
53 # Generic inline with unique_together
54 #
55 
56diff --git a/tests/regressiontests/generic_inline_admin/tests.py b/tests/regressiontests/generic_inline_admin/tests.py
57index c40b6dd..4a151bd 100644
58--- a/tests/regressiontests/generic_inline_admin/tests.py
59+++ b/tests/regressiontests/generic_inline_admin/tests.py
60@@ -6,7 +6,8 @@ from django.test import TestCase
61 
62 # local test models
63 from models import Episode, EpisodeExtra, EpisodeMaxNum, EpisodeExclude, \
64-                   Media, EpisodePermanent, MediaPermanentInline, Category
65+                   EpisodeModelFormExclude, Media, EpisodePermanent, \
66+                   MediaPermanentInline, Category
67 
68 
69 class GenericAdminViewTest(TestCase):
70@@ -181,6 +182,15 @@ class GenericInlineAdminParametersTest(TestCase):
71         formset = response.context['inline_admin_formsets'][0].formset
72         self.assertFalse('url' in formset.forms[0], 'The formset has excluded "url" field.')
73 
74+    def testModelFormExcludeParam(self):
75+        """
76+        Generic inline formsets should respect exclude on custom model forms.
77+        """
78+        e = self._create_object(EpisodeModelFormExclude)
79+        response = self.client.get('/generic_inline_admin/admin/generic_inline_admin/episodemodelformexclude/%s/' % e.pk)
80+        formset = response.context['inline_admin_formsets'][0].formset
81+        self.assertFalse('url' in formset.forms[0].fields, 'The formset has excluded "url" field.')
82+
83 class GenericInlineAdminWithUniqueTogetherTest(TestCase):
84     fixtures = ['users.xml']
85