Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #15907 -- Fixed another conflict between the ModelForm exclude …

…and the GenericInline. Thanks, leonelfreire and prestontimmons.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16603 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 386b12c1c668dd54d24704ef974e17b38abb825d 1 parent 3d027b7
@jezdez jezdez authored
View
4 django/contrib/contenttypes/generic.py
@@ -406,6 +406,10 @@ def get_formset(self, request, obj=None, **kwargs):
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,
View
16 tests/regressiontests/generic_inline_admin/models.py
@@ -5,6 +5,8 @@
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
View
102 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 @@ def testBasicEditPost(self):
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 @@ def testGenericInlineFormset(self):
self.assertEqual(formset.forms[1].as_p(), '<p><label for="id_generic_inline_admin-media-content_type-object_id-1-url">Url:</label> <input id="id_generic_inline_admin-media-content_type-object_id-1-url" type="text" name="generic_inline_admin-media-content_type-object_id-1-url" value="http://example.com/podcast.mp3" maxlength="200" /><input type="hidden" name="generic_inline_admin-media-content_type-object_id-1-id" value="%s" id="id_generic_inline_admin-media-content_type-object_id-1-id" /></p>' % self.mp3_media_pk)
self.assertEqual(formset.forms[2].as_p(), '<p><label for="id_generic_inline_admin-media-content_type-object_id-2-url">Url:</label> <input id="id_generic_inline_admin-media-content_type-object_id-2-url" type="text" name="generic_inline_admin-media-content_type-object_id-2-url" maxlength="200" /><input type="hidden" name="generic_inline_admin-media-content_type-object_id-2-id" id="id_generic_inline_admin-media-content_type-object_id-2-id" /></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 @@ def testMaxNumParam(self):
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 @@ def test_no_deletion(self):
class GenericInlineModelAdminTest(TestCase):
+ def setUp(self):
+ self.site = AdminSite()
+
def test_get_formset_kwargs(self):
media_inline = MediaInline(Media, AdminSite())
@@ -230,3 +227,82 @@ def test_get_formset_kwargs(self):
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'])
View
2  tests/regressiontests/generic_views/edit.py
@@ -10,7 +10,7 @@
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'
Please sign in to comment.
Something went wrong with that request. Please try again.