diff --git a/django/contrib/contenttypes/generic.py b/django/contrib/contenttypes/generic.py index dc58f1f6f1..69dd527037 100644 --- a/django/contrib/contenttypes/generic.py +++ b/django/contrib/contenttypes/generic.py @@ -406,6 +406,10 @@ class GenericInlineModelAdmin(InlineModelAdmin): else: exclude = list(self.exclude) exclude.extend(self.get_readonly_fields(request, obj)) + if self.exclude is None and hasattr(self.form, '_meta') and self.form._meta.exclude: + # Take the custom ModelForm's Meta.exclude into account only if the + # GenericInlineModelAdmin doesn't define its own. + exclude.extend(self.form._meta.exclude) exclude = exclude or None defaults = { "ct_field": self.ct_field, diff --git a/tests/regressiontests/generic_inline_admin/models.py b/tests/regressiontests/generic_inline_admin/models.py index fb6363e2bb..32ecd3b889 100644 --- a/tests/regressiontests/generic_inline_admin/models.py +++ b/tests/regressiontests/generic_inline_admin/models.py @@ -5,6 +5,8 @@ from django.contrib.contenttypes.models import ContentType class Episode(models.Model): name = models.CharField(max_length=100) + length = models.CharField(max_length=100, blank=True) + author = models.CharField(max_length=100, blank=True) class Media(models.Model): """ @@ -14,6 +16,8 @@ class Media(models.Model): object_id = models.PositiveIntegerField() content_object = generic.GenericForeignKey() url = models.URLField(verify_exists=False) + description = models.CharField(max_length=100, blank=True) + keywords = models.CharField(max_length=100, blank=True) def __unicode__(self): return self.url @@ -59,18 +63,6 @@ class MediaMaxNumInline(generic.GenericTabularInline): admin.site.register(EpisodeMaxNum, inlines=[MediaMaxNumInline]) -# -# Generic inline with exclude -# - -class EpisodeExclude(Episode): - pass - -class MediaExcludeInline(generic.GenericTabularInline): - model = Media - exclude = ['url'] - -admin.site.register(EpisodeExclude, inlines=[MediaExcludeInline]) # # Generic inline with unique_together diff --git a/tests/regressiontests/generic_inline_admin/tests.py b/tests/regressiontests/generic_inline_admin/tests.py index 00badc9228..da59922fe5 100644 --- a/tests/regressiontests/generic_inline_admin/tests.py +++ b/tests/regressiontests/generic_inline_admin/tests.py @@ -1,13 +1,16 @@ # coding: utf-8 from django.conf import settings +from django.contrib import admin from django.contrib.admin.sites import AdminSite -from django.contrib.contenttypes.generic import generic_inlineformset_factory +from django.contrib.contenttypes.generic import ( + generic_inlineformset_factory, GenericTabularInline) +from django.forms.models import ModelForm from django.test import TestCase # local test models -from models import (Episode, EpisodeExtra, EpisodeMaxNum, EpisodeExclude, - Media, MediaInline, EpisodePermanent, MediaPermanentInline, Category) +from models import (Episode, EpisodeExtra, EpisodeMaxNum, Media, + MediaInline, EpisodePermanent, MediaPermanentInline, Category) class GenericAdminViewTest(TestCase): @@ -88,7 +91,7 @@ class GenericAdminViewTest(TestCase): self.assertEqual(response.status_code, 302) # redirect somewhere def testGenericInlineFormset(self): - EpisodeMediaFormSet = generic_inlineformset_factory(Media, can_delete=False, extra=3) + EpisodeMediaFormSet = generic_inlineformset_factory(Media, can_delete=False, exclude=['description', 'keywords'], extra=3) e = Episode.objects.get(name='This Week in Django') # Works with no queryset @@ -105,7 +108,6 @@ class GenericAdminViewTest(TestCase): self.assertEqual(formset.forms[1].as_p(), '

' % self.mp3_media_pk) self.assertEqual(formset.forms[2].as_p(), '

') - # Works with a queryset that omits items formset = EpisodeMediaFormSet(instance=e, queryset=Media.objects.filter(url__endswith=".png")) self.assertEqual(len(formset.forms), 4) @@ -173,14 +175,6 @@ class GenericInlineAdminParametersTest(TestCase): self.assertEqual(formset.total_form_count(), 2) self.assertEqual(formset.initial_form_count(), 1) - def testExcludeParam(self): - """ - Generic inline formsets should respect include. - """ - e = self._create_object(EpisodeExclude) - response = self.client.get('/generic_inline_admin/admin/generic_inline_admin/episodeexclude/%s/' % e.pk) - formset = response.context['inline_admin_formsets'][0].formset - self.assertFalse('url' in formset.forms[0], 'The formset has excluded "url" field.') class GenericInlineAdminWithUniqueTogetherTest(TestCase): fixtures = ['users.xml'] @@ -218,6 +212,9 @@ class NoInlineDeletionTest(TestCase): class GenericInlineModelAdminTest(TestCase): + def setUp(self): + self.site = AdminSite() + def test_get_formset_kwargs(self): media_inline = MediaInline(Media, AdminSite()) @@ -230,3 +227,82 @@ class GenericInlineModelAdminTest(TestCase): formset = media_inline.get_formset(None, max_num=100, can_order=True) self.assertEqual(formset.max_num, 100) self.assertEqual(formset.can_order, True) + + def test_custom_form_meta_exclude_with_readonly(self): + """ + Ensure that the custom ModelForm's `Meta.exclude` is respected when + used in conjunction with `GenericInlineModelAdmin.readonly_fields` + and when no `ModelAdmin.exclude` is defined. + """ + + request = None + + class MediaForm(ModelForm): + + class Meta: + model = Media + exclude = ['url'] + + class MediaInline(GenericTabularInline): + readonly_fields = ['description'] + form = MediaForm + model = Media + + class EpisodeAdmin(admin.ModelAdmin): + inlines = [ + MediaInline + ] + + ma = EpisodeAdmin(Episode, self.site) + self.assertEqual( + list(ma.get_formsets(request))[0]().forms[0].fields.keys(), + ['keywords', 'id', 'DELETE']) + + def test_custom_form_meta_exclude(self): + """ + Ensure that the custom ModelForm's `Meta.exclude` is respected by + `GenericInlineModelAdmin.get_formset`, and overridden if + `ModelAdmin.exclude` or `GenericInlineModelAdmin.exclude` are defined. + Refs #15907. + """ + + request = None + + # First with `GenericInlineModelAdmin` ----------------- + + class MediaForm(ModelForm): + + class Meta: + model = Media + exclude = ['url'] + + class MediaInline(GenericTabularInline): + exclude = ['description'] + form = MediaForm + model = Media + + class EpisodeAdmin(admin.ModelAdmin): + inlines = [ + MediaInline + ] + + ma = EpisodeAdmin(Episode, self.site) + self.assertEqual( + list(ma.get_formsets(request))[0]().forms[0].fields.keys(), + ['url', 'keywords', 'id', 'DELETE']) + + # Then, only with `ModelForm` ----------------- + + class MediaInline(GenericTabularInline): + form = MediaForm + model = Media + + class EpisodeAdmin(admin.ModelAdmin): + inlines = [ + MediaInline + ] + + ma = EpisodeAdmin(Episode, self.site) + self.assertEqual( + list(ma.get_formsets(request))[0]().forms[0].fields.keys(), + ['description', 'keywords', 'id', 'DELETE']) diff --git a/tests/regressiontests/generic_views/edit.py b/tests/regressiontests/generic_views/edit.py index f542a68a0d..0adcb15afa 100644 --- a/tests/regressiontests/generic_views/edit.py +++ b/tests/regressiontests/generic_views/edit.py @@ -10,7 +10,7 @@ from regressiontests.generic_views import views class ModelFormMixinTests(TestCase): def test_get_form(self): form_class = views.AuthorGetQuerySetFormView().get_form_class() - self.assertEqual(form_class.Meta.model, Author) + self.assertEqual(form_class._meta.model, Author) class CreateViewTests(TestCase): urls = 'regressiontests.generic_views.urls'