Ticket #19423: 19423-1.diff

File 19423-1.diff, 3.2 KB (added by Claude Paroz, 12 years ago)
  • django/contrib/admin/options.py

    diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
    index c48ffa6..f4c2751 100644
    a b  
     1import copy
    12from functools import update_wrapper, partial
    23import warnings
    34
    class BaseModelAdmin(six.with_metaclass(forms.MediaDefiningClass)):  
    130131        # passed to formfield_for_dbfield override the defaults.
    131132        for klass in db_field.__class__.mro():
    132133            if klass in self.formfield_overrides:
    133                 kwargs = dict(self.formfield_overrides[klass], **kwargs)
     134                kwargs = dict(copy.deepcopy(self.formfield_overrides[klass]), **kwargs)
    134135                return db_field.formfield(**kwargs)
    135136
    136137        # For any other type of field, just call its formfield() method.
  • tests/regressiontests/admin_widgets/models.py

    diff --git a/tests/regressiontests/admin_widgets/models.py b/tests/regressiontests/admin_widgets/models.py
    index 2e45279..2977b86 100644
    a b class Member(models.Model):  
    2020@python_2_unicode_compatible
    2121class Band(models.Model):
    2222    name = models.CharField(max_length=100)
     23    style = models.CharField(max_length=20)
    2324    members = models.ManyToManyField(Member)
    2425
    2526    def __str__(self):
  • tests/regressiontests/admin_widgets/tests.py

    diff --git a/tests/regressiontests/admin_widgets/tests.py b/tests/regressiontests/admin_widgets/tests.py
    index 0b016d8..76aa9f4 100644
    a b from django.contrib.admin import widgets  
    1010from django.contrib.admin.tests import AdminSeleniumWebDriverTestCase
    1111from django.core.files.storage import default_storage
    1212from django.core.files.uploadedfile import SimpleUploadedFile
    13 from django.db.models import DateField
     13from django.db.models import CharField, DateField
    1414from django.test import TestCase as DjangoTestCase
    1515from django.test.utils import override_settings
    1616from django.utils import translation
    class AdminFormfieldForDBFieldTests(TestCase):  
    112112        self.assertFormfield(models.Event, 'start_date', forms.TextInput,
    113113                             formfield_overrides={DateField: {'widget': forms.TextInput}})
    114114
     115    def testFormfieldOverridesWidgetInstances(self):
     116        """
     117        Test that widget instances in formfield_overrides are not shared between
     118        different fields. (#19423)
     119        """
     120        class BandAdmin(admin.ModelAdmin):
     121            formfield_overrides = {
     122                CharField: {'widget': forms.TextInput(attrs={'size':'10'})}
     123            }
     124        ma = BandAdmin(models.Band, admin.site)
     125        f1 = ma.formfield_for_dbfield(models.Band._meta.get_field('name'), request=None)
     126        f2 = ma.formfield_for_dbfield(models.Band._meta.get_field('style'), request=None)
     127        self.assertNotEqual(f1.widget, f2.widget)
     128        self.assertEqual(f1.widget.attrs['maxlength'], '100')
     129        self.assertEqual(f2.widget.attrs['maxlength'], '20')
     130        self.assertEqual(f2.widget.attrs['size'], '10')
     131
    115132    def testFieldWithChoices(self):
    116133        self.assertFormfield(models.Member, 'gender', forms.Select)
    117134
Back to Top