Ticket #15907: 15907_patch_at_r16141.diff

File 15907_patch_at_r16141.diff, 3.4 KB (added by prestontimmons, 4 years ago)

Patch combined with tests

  • django/forms/models.py

    diff --git a/django/forms/models.py b/django/forms/models.py
    index ecfdc92..4862c8d 100644
    a b def modelform_factory(model, form=ModelForm, fields=None, exclude=None, 
    384384    # creating needs to inherit from the parent's inner meta.
    385385    parent = (object,)
    386386    if hasattr(form, 'Meta'):
     387        if 'exclude' in attrs and hasattr(form.Meta, 'exclude'):
     388            all_exclude = tuple(attrs['exclude']) + tuple(form.Meta.exclude)
     389            attrs['exclude'] = tuple(set(all_exclude))
    387390        parent = (form.Meta, object)
    388391    Meta = type('Meta', parent, attrs)
    389392
  • 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..ed412bf 100644
    a b from django.db import models 
    22from django.contrib import admin
    33from django.contrib.contenttypes import generic
    44from django.contrib.contenttypes.models import ContentType
     5from django.forms.models import ModelForm
    56
    67class Episode(models.Model):
    78    name = models.CharField(max_length=100)
    class MediaExcludeInline(generic.GenericTabularInline): 
    7374admin.site.register(EpisodeExclude, inlines=[MediaExcludeInline])
    7475
    7576#
     77# Generic inline with custom modelform and exclude
     78#
     79
     80class EpisodeModelFormExclude(Episode):
     81    pass
     82
     83class ModelFormWithExclude(ModelForm):
     84    class Meta:
     85        model = Media
     86        exclude = ('url',)
     87
     88class MediaWithModelFormExcludeInline(generic.GenericTabularInline):
     89    form = ModelFormWithExclude
     90    model = Media
     91    extra = 1
     92
     93class EpisodeModelFormExcludeAdmin(admin.ModelAdmin):
     94    inlines = (MediaWithModelFormExcludeInline, )
     95
     96admin.site.register(EpisodeModelFormExclude, EpisodeModelFormExcludeAdmin)
     97
     98#
    7699# Generic inline with unique_together
    77100#
    78101
  • 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 c40b6dd..4a151bd 100644
    a b from django.test import TestCase 
    66
    77# local test models
    88from models import Episode, EpisodeExtra, EpisodeMaxNum, EpisodeExclude, \
    9                    Media, EpisodePermanent, MediaPermanentInline, Category
     9                   EpisodeModelFormExclude, Media, EpisodePermanent, \
     10                   MediaPermanentInline, Category
    1011
    1112
    1213class GenericAdminViewTest(TestCase):
    class GenericInlineAdminParametersTest(TestCase): 
    181182        formset = response.context['inline_admin_formsets'][0].formset
    182183        self.assertFalse('url' in formset.forms[0], 'The formset has excluded "url" field.')
    183184
     185    def testModelFormExcludeParam(self):
     186        """
     187        Generic inline formsets should respect exclude on custom model forms.
     188        """
     189        e = self._create_object(EpisodeModelFormExclude)
     190        response = self.client.get('/generic_inline_admin/admin/generic_inline_admin/episodemodelformexclude/%s/' % e.pk)
     191        formset = response.context['inline_admin_formsets'][0].formset
     192        self.assertFalse('url' in formset.forms[0].fields, 'The formset has excluded "url" field.')
     193
    184194class GenericInlineAdminWithUniqueTogetherTest(TestCase):
    185195    fixtures = ['users.xml']
    186196
Back to Top