Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #10059: `ModelAdmin.formfield_for_dbfield` now handles custom `…

…Field` subclasses. Thanks, Alex Gaynor.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10454 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit f55f2b9d74bf59d1b3ace5df5f498998eb62cb28 1 parent 6eaf154
Jacob Kaplan-Moss authored April 08, 2009
7  django/contrib/admin/options.py
@@ -108,9 +108,10 @@ def formfield_for_dbfield(self, db_field, **kwargs):
108 108
 
109 109
         # If we've got overrides for the formfield defined, use 'em. **kwargs
110 110
         # passed to formfield_for_dbfield override the defaults.
111  
-        if db_field.__class__ in self.formfield_overrides:
112  
-            kwargs = dict(self.formfield_overrides[db_field.__class__], **kwargs)
113  
-            return db_field.formfield(**kwargs)
  111
+        for klass in db_field.__class__.mro(): 
  112
+            if klass in self.formfield_overrides: 
  113
+                kwargs = dict(self.formfield_overrides[klass], **kwargs) 
  114
+                return db_field.formfield(**kwargs) 
114 115
 
115 116
         # For any other type of field, just call its formfield() method.
116 117
         return db_field.formfield(**kwargs)
4  tests/regressiontests/admin_widgets/models.py
@@ -4,6 +4,9 @@
4 4
 from django.core.files.storage import default_storage
5 5
 from django.contrib.auth.models import User
6 6
 
  7
+class MyFileField(models.FileField): 
  8
+    pass 
  9
+
7 10
 class Member(models.Model):
8 11
     name = models.CharField(max_length=100)
9 12
     birthdate = models.DateTimeField(blank=True, null=True)
@@ -23,6 +26,7 @@ class Album(models.Model):
23 26
     band = models.ForeignKey(Band)
24 27
     name = models.CharField(max_length=100)
25 28
     cover_art = models.FileField(upload_to='albums')
  29
+    backside_art = MyFileField(upload_to='albums_back', null=True)
26 30
 
27 31
     def __unicode__(self):
28 32
         return self.name
8  tests/regressiontests/admin_widgets/tests.py
@@ -98,6 +98,8 @@ def testChoicesWithRadioFields(self):
98 98
         self.assertFormfield(models.Member, 'gender', widgets.AdminRadioSelect,
99 99
                              radio_fields={'gender':admin.VERTICAL})
100 100
 
  101
+    def testInheritance(self):
  102
+        self.assertFormfield(models.Album, 'backside_art', widgets.AdminFileWidget)
101 103
 
102 104
 class AdminFormfieldForDBFieldWithRequestTests(DjangoTestCase):
103 105
     fixtures = ["admin-widgets-users.xml"]
@@ -113,13 +115,13 @@ def testFilterChoicesByRequestUser(self):
113 115
 
114 116
 class AdminForeignKeyWidgetChangeList(DjangoTestCase):
115 117
     fixtures = ["admin-widgets-users.xml"]
116  
-    
  118
+
117 119
     def setUp(self):
118 120
         self.client.login(username="super", password="secret")
119  
-    
  121
+
120 122
     def tearDown(self):
121 123
         self.client.logout()
122  
-    
  124
+
123 125
     def test_changelist_foreignkey(self):
124 126
         response = self.client.get('/widget_admin/admin_widgets/car/')
125 127
         self.failUnless('/widget_admin/auth/user/add/' in response.content)

0 notes on commit f55f2b9

Please sign in to comment.
Something went wrong with that request. Please try again.