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
This commit is contained in:
Jacob Kaplan-Moss 2009-04-08 20:25:56 +00:00
parent 6eaf154a2e
commit f55f2b9d74
3 changed files with 13 additions and 6 deletions

View File

@ -108,9 +108,10 @@ class BaseModelAdmin(object):
# If we've got overrides for the formfield defined, use 'em. **kwargs # If we've got overrides for the formfield defined, use 'em. **kwargs
# passed to formfield_for_dbfield override the defaults. # passed to formfield_for_dbfield override the defaults.
if db_field.__class__ in self.formfield_overrides: for klass in db_field.__class__.mro():
kwargs = dict(self.formfield_overrides[db_field.__class__], **kwargs) if klass in self.formfield_overrides:
return db_field.formfield(**kwargs) kwargs = dict(self.formfield_overrides[klass], **kwargs)
return db_field.formfield(**kwargs)
# For any other type of field, just call its formfield() method. # For any other type of field, just call its formfield() method.
return db_field.formfield(**kwargs) return db_field.formfield(**kwargs)

View File

@ -4,6 +4,9 @@ from django.db import models
from django.core.files.storage import default_storage from django.core.files.storage import default_storage
from django.contrib.auth.models import User from django.contrib.auth.models import User
class MyFileField(models.FileField):
pass
class Member(models.Model): class Member(models.Model):
name = models.CharField(max_length=100) name = models.CharField(max_length=100)
birthdate = models.DateTimeField(blank=True, null=True) birthdate = models.DateTimeField(blank=True, null=True)
@ -23,6 +26,7 @@ class Album(models.Model):
band = models.ForeignKey(Band) band = models.ForeignKey(Band)
name = models.CharField(max_length=100) name = models.CharField(max_length=100)
cover_art = models.FileField(upload_to='albums') cover_art = models.FileField(upload_to='albums')
backside_art = MyFileField(upload_to='albums_back', null=True)
def __unicode__(self): def __unicode__(self):
return self.name return self.name

View File

@ -98,6 +98,8 @@ class AdminFormfieldForDBFieldTests(TestCase):
self.assertFormfield(models.Member, 'gender', widgets.AdminRadioSelect, self.assertFormfield(models.Member, 'gender', widgets.AdminRadioSelect,
radio_fields={'gender':admin.VERTICAL}) radio_fields={'gender':admin.VERTICAL})
def testInheritance(self):
self.assertFormfield(models.Album, 'backside_art', widgets.AdminFileWidget)
class AdminFormfieldForDBFieldWithRequestTests(DjangoTestCase): class AdminFormfieldForDBFieldWithRequestTests(DjangoTestCase):
fixtures = ["admin-widgets-users.xml"] fixtures = ["admin-widgets-users.xml"]
@ -113,13 +115,13 @@ class AdminFormfieldForDBFieldWithRequestTests(DjangoTestCase):
class AdminForeignKeyWidgetChangeList(DjangoTestCase): class AdminForeignKeyWidgetChangeList(DjangoTestCase):
fixtures = ["admin-widgets-users.xml"] fixtures = ["admin-widgets-users.xml"]
def setUp(self): def setUp(self):
self.client.login(username="super", password="secret") self.client.login(username="super", password="secret")
def tearDown(self): def tearDown(self):
self.client.logout() self.client.logout()
def test_changelist_foreignkey(self): def test_changelist_foreignkey(self):
response = self.client.get('/widget_admin/admin_widgets/car/') response = self.client.get('/widget_admin/admin_widgets/car/')
self.failUnless('/widget_admin/auth/user/add/' in response.content) self.failUnless('/widget_admin/auth/user/add/' in response.content)