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
This commit is contained in:
Jannis Leidel 2011-08-12 14:14:58 +00:00
parent 3d027b72eb
commit 386b12c1c6
4 changed files with 98 additions and 26 deletions

View File

@ -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,

View File

@ -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

View File

@ -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(), '<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 @@ 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'])

View File

@ -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'