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:
parent
3d027b72eb
commit
386b12c1c6
|
@ -406,6 +406,10 @@ class GenericInlineModelAdmin(InlineModelAdmin):
|
||||||
else:
|
else:
|
||||||
exclude = list(self.exclude)
|
exclude = list(self.exclude)
|
||||||
exclude.extend(self.get_readonly_fields(request, obj))
|
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
|
exclude = exclude or None
|
||||||
defaults = {
|
defaults = {
|
||||||
"ct_field": self.ct_field,
|
"ct_field": self.ct_field,
|
||||||
|
|
|
@ -5,6 +5,8 @@ from django.contrib.contenttypes.models import ContentType
|
||||||
|
|
||||||
class Episode(models.Model):
|
class Episode(models.Model):
|
||||||
name = models.CharField(max_length=100)
|
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):
|
class Media(models.Model):
|
||||||
"""
|
"""
|
||||||
|
@ -14,6 +16,8 @@ class Media(models.Model):
|
||||||
object_id = models.PositiveIntegerField()
|
object_id = models.PositiveIntegerField()
|
||||||
content_object = generic.GenericForeignKey()
|
content_object = generic.GenericForeignKey()
|
||||||
url = models.URLField(verify_exists=False)
|
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):
|
def __unicode__(self):
|
||||||
return self.url
|
return self.url
|
||||||
|
@ -59,18 +63,6 @@ class MediaMaxNumInline(generic.GenericTabularInline):
|
||||||
|
|
||||||
admin.site.register(EpisodeMaxNum, inlines=[MediaMaxNumInline])
|
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
|
# Generic inline with unique_together
|
||||||
|
|
|
@ -1,13 +1,16 @@
|
||||||
# coding: utf-8
|
# coding: utf-8
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
from django.contrib import admin
|
||||||
from django.contrib.admin.sites import AdminSite
|
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
|
from django.test import TestCase
|
||||||
|
|
||||||
# local test models
|
# local test models
|
||||||
from models import (Episode, EpisodeExtra, EpisodeMaxNum, EpisodeExclude,
|
from models import (Episode, EpisodeExtra, EpisodeMaxNum, Media,
|
||||||
Media, MediaInline, EpisodePermanent, MediaPermanentInline, Category)
|
MediaInline, EpisodePermanent, MediaPermanentInline, Category)
|
||||||
|
|
||||||
|
|
||||||
class GenericAdminViewTest(TestCase):
|
class GenericAdminViewTest(TestCase):
|
||||||
|
@ -88,7 +91,7 @@ class GenericAdminViewTest(TestCase):
|
||||||
self.assertEqual(response.status_code, 302) # redirect somewhere
|
self.assertEqual(response.status_code, 302) # redirect somewhere
|
||||||
|
|
||||||
def testGenericInlineFormset(self):
|
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')
|
e = Episode.objects.get(name='This Week in Django')
|
||||||
|
|
||||||
# Works with no queryset
|
# 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[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>')
|
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
|
# Works with a queryset that omits items
|
||||||
formset = EpisodeMediaFormSet(instance=e, queryset=Media.objects.filter(url__endswith=".png"))
|
formset = EpisodeMediaFormSet(instance=e, queryset=Media.objects.filter(url__endswith=".png"))
|
||||||
self.assertEqual(len(formset.forms), 4)
|
self.assertEqual(len(formset.forms), 4)
|
||||||
|
@ -173,14 +175,6 @@ class GenericInlineAdminParametersTest(TestCase):
|
||||||
self.assertEqual(formset.total_form_count(), 2)
|
self.assertEqual(formset.total_form_count(), 2)
|
||||||
self.assertEqual(formset.initial_form_count(), 1)
|
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):
|
class GenericInlineAdminWithUniqueTogetherTest(TestCase):
|
||||||
fixtures = ['users.xml']
|
fixtures = ['users.xml']
|
||||||
|
@ -218,6 +212,9 @@ class NoInlineDeletionTest(TestCase):
|
||||||
|
|
||||||
class GenericInlineModelAdminTest(TestCase):
|
class GenericInlineModelAdminTest(TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
self.site = AdminSite()
|
||||||
|
|
||||||
def test_get_formset_kwargs(self):
|
def test_get_formset_kwargs(self):
|
||||||
media_inline = MediaInline(Media, AdminSite())
|
media_inline = MediaInline(Media, AdminSite())
|
||||||
|
|
||||||
|
@ -230,3 +227,82 @@ class GenericInlineModelAdminTest(TestCase):
|
||||||
formset = media_inline.get_formset(None, max_num=100, can_order=True)
|
formset = media_inline.get_formset(None, max_num=100, can_order=True)
|
||||||
self.assertEqual(formset.max_num, 100)
|
self.assertEqual(formset.max_num, 100)
|
||||||
self.assertEqual(formset.can_order, True)
|
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'])
|
||||||
|
|
|
@ -10,7 +10,7 @@ from regressiontests.generic_views import views
|
||||||
class ModelFormMixinTests(TestCase):
|
class ModelFormMixinTests(TestCase):
|
||||||
def test_get_form(self):
|
def test_get_form(self):
|
||||||
form_class = views.AuthorGetQuerySetFormView().get_form_class()
|
form_class = views.AuthorGetQuerySetFormView().get_form_class()
|
||||||
self.assertEqual(form_class.Meta.model, Author)
|
self.assertEqual(form_class._meta.model, Author)
|
||||||
|
|
||||||
class CreateViewTests(TestCase):
|
class CreateViewTests(TestCase):
|
||||||
urls = 'regressiontests.generic_views.urls'
|
urls = 'regressiontests.generic_views.urls'
|
||||||
|
|
Loading…
Reference in New Issue