Ticket #15907: 15907.generic-inline-admin-fix.diff

File 15907.generic-inline-admin-fix.diff, 8.4 KB (added by Preston Timmons, 13 years ago)
  • django/contrib/contenttypes/generic.py

    diff --git a/django/contrib/contenttypes/generic.py b/django/contrib/contenttypes/generic.py
    index 389692d..c6d9f30 100644
    a b class GenericInlineModelAdmin(InlineModelAdmin):  
    406406        else:
    407407            exclude = list(self.exclude)
    408408        exclude.extend(self.get_readonly_fields(request, obj))
     409        if self.exclude is None and hasattr(self.form, '_meta') and self.form._meta.exclude:
     410            # Take the custom ModelForm's Meta.exclude into account only if the
     411            # GenericInlineModelAdmin doesn't define its own.
     412            exclude.extend(self.form.Meta.exclude)
    409413        exclude = exclude or None
    410414        defaults = {
    411415            "ct_field": self.ct_field,
  • tests/regressiontests/generic_inline_admin/models.py

    diff --git a/tests/regressiontests/generic_inline_admin/models.py b/tests/regressiontests/generic_inline_admin/models.py
    index fb6363e..32ecd3b 100644
    a b from django.contrib.contenttypes.models import ContentType  
    55
    66class Episode(models.Model):
    77    name = models.CharField(max_length=100)
     8    length = models.CharField(max_length=100, blank=True)
     9    author = models.CharField(max_length=100, blank=True)
    810
    911class Media(models.Model):
    1012    """
    class Media(models.Model):  
    1416    object_id = models.PositiveIntegerField()
    1517    content_object = generic.GenericForeignKey()
    1618    url = models.URLField(verify_exists=False)
     19    description = models.CharField(max_length=100, blank=True)
     20    keywords = models.CharField(max_length=100, blank=True)
    1721
    1822    def __unicode__(self):
    1923        return self.url
    class MediaMaxNumInline(generic.GenericTabularInline):  
    5963
    6064admin.site.register(EpisodeMaxNum, inlines=[MediaMaxNumInline])
    6165
    62 #
    63 # Generic inline with exclude
    64 #
    65 
    66 class EpisodeExclude(Episode):
    67     pass
    68 
    69 class MediaExcludeInline(generic.GenericTabularInline):
    70     model = Media
    71     exclude = ['url']
    72 
    73 admin.site.register(EpisodeExclude, inlines=[MediaExcludeInline])
    7466
    7567#
    7668# Generic inline with unique_together
  • tests/regressiontests/generic_inline_admin/tests.py

    diff --git a/tests/regressiontests/generic_inline_admin/tests.py b/tests/regressiontests/generic_inline_admin/tests.py
    index 00badc9..da59922 100644
    a b  
    11# coding: utf-8
    22
    33from django.conf import settings
     4from django.contrib import admin
    45from django.contrib.admin.sites import AdminSite
    5 from django.contrib.contenttypes.generic import generic_inlineformset_factory
     6from django.contrib.contenttypes.generic import (
     7    generic_inlineformset_factory, GenericTabularInline)
     8from django.forms.models import ModelForm
    69from django.test import TestCase
    710
    811# local test models
    9 from models import (Episode, EpisodeExtra, EpisodeMaxNum, EpisodeExclude,
    10     Media, MediaInline, EpisodePermanent, MediaPermanentInline, Category)
     12from models import (Episode, EpisodeExtra, EpisodeMaxNum, Media,
     13    MediaInline, EpisodePermanent, MediaPermanentInline, Category)
    1114
    1215
    1316class GenericAdminViewTest(TestCase):
    class GenericAdminViewTest(TestCase):  
    8891        self.assertEqual(response.status_code, 302) # redirect somewhere
    8992
    9093    def testGenericInlineFormset(self):
    91         EpisodeMediaFormSet = generic_inlineformset_factory(Media, can_delete=False, extra=3)
     94        EpisodeMediaFormSet = generic_inlineformset_factory(Media, can_delete=False, exclude=['description', 'keywords'], extra=3)
    9295        e = Episode.objects.get(name='This Week in Django')
    9396
    9497        # Works with no queryset
    class GenericAdminViewTest(TestCase):  
    105108        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)
    106109        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>')
    107110
    108 
    109111        # Works with a queryset that omits items
    110112        formset = EpisodeMediaFormSet(instance=e, queryset=Media.objects.filter(url__endswith=".png"))
    111113        self.assertEqual(len(formset.forms), 4)
    class GenericInlineAdminParametersTest(TestCase):  
    173175        self.assertEqual(formset.total_form_count(), 2)
    174176        self.assertEqual(formset.initial_form_count(), 1)
    175177
    176     def testExcludeParam(self):
    177         """
    178         Generic inline formsets should respect include.
    179         """
    180         e = self._create_object(EpisodeExclude)
    181         response = self.client.get('/generic_inline_admin/admin/generic_inline_admin/episodeexclude/%s/' % e.pk)
    182         formset = response.context['inline_admin_formsets'][0].formset
    183         self.assertFalse('url' in formset.forms[0], 'The formset has excluded "url" field.')
    184178
    185179class GenericInlineAdminWithUniqueTogetherTest(TestCase):
    186180    fixtures = ['users.xml']
    class NoInlineDeletionTest(TestCase):  
    218212
    219213class GenericInlineModelAdminTest(TestCase):
    220214
     215    def setUp(self):
     216        self.site = AdminSite()
     217
    221218    def test_get_formset_kwargs(self):
    222219        media_inline = MediaInline(Media, AdminSite())
    223220
    class GenericInlineModelAdminTest(TestCase):  
    230227        formset = media_inline.get_formset(None, max_num=100, can_order=True)
    231228        self.assertEqual(formset.max_num, 100)
    232229        self.assertEqual(formset.can_order, True)
     230
     231    def test_custom_form_meta_exclude_with_readonly(self):
     232        """
     233        Ensure that the custom ModelForm's `Meta.exclude` is respected when
     234        used in conjunction with `GenericInlineModelAdmin.readonly_fields`
     235        and when no `ModelAdmin.exclude` is defined.
     236        """
     237
     238        request = None
     239
     240        class MediaForm(ModelForm):
     241
     242            class Meta:
     243                model = Media
     244                exclude = ['url']
     245
     246        class MediaInline(GenericTabularInline):
     247            readonly_fields = ['description']
     248            form = MediaForm
     249            model = Media
     250
     251        class EpisodeAdmin(admin.ModelAdmin):
     252            inlines = [
     253                MediaInline
     254            ]
     255
     256        ma = EpisodeAdmin(Episode, self.site)
     257        self.assertEqual(
     258            list(ma.get_formsets(request))[0]().forms[0].fields.keys(),
     259            ['keywords', 'id', 'DELETE'])
     260
     261    def test_custom_form_meta_exclude(self):
     262        """
     263        Ensure that the custom ModelForm's `Meta.exclude` is respected by
     264        `GenericInlineModelAdmin.get_formset`, and overridden if
     265        `ModelAdmin.exclude` or `GenericInlineModelAdmin.exclude` are defined.
     266        Refs #15907.
     267        """
     268
     269        request = None
     270
     271        # First with `GenericInlineModelAdmin`  -----------------
     272
     273        class MediaForm(ModelForm):
     274
     275            class Meta:
     276                model = Media
     277                exclude = ['url']
     278
     279        class MediaInline(GenericTabularInline):
     280            exclude = ['description']
     281            form = MediaForm
     282            model = Media
     283
     284        class EpisodeAdmin(admin.ModelAdmin):
     285            inlines = [
     286                MediaInline
     287            ]
     288
     289        ma = EpisodeAdmin(Episode, self.site)
     290        self.assertEqual(
     291            list(ma.get_formsets(request))[0]().forms[0].fields.keys(),
     292            ['url', 'keywords', 'id', 'DELETE'])
     293
     294        # Then, only with `ModelForm`  -----------------
     295
     296        class MediaInline(GenericTabularInline):
     297            form = MediaForm
     298            model = Media
     299
     300        class EpisodeAdmin(admin.ModelAdmin):
     301            inlines = [
     302                MediaInline
     303            ]
     304
     305        ma = EpisodeAdmin(Episode, self.site)
     306        self.assertEqual(
     307            list(ma.get_formsets(request))[0]().forms[0].fields.keys(),
     308            ['description', 'keywords', 'id', 'DELETE'])
Back to Top