Skip to content

Commit

Permalink
Fixed #12004 -- Improved error reporting when an abstract class is re…
Browse files Browse the repository at this point in the history
…gistered with the admin. Thanks to Matt Smalley for the report, and to mk and Julien Phalip for the patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@15636 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
freakboy3742 committed Feb 23, 2011
1 parent 7aa8491 commit 12bd7bc
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
6 changes: 6 additions & 0 deletions django/contrib/admin/sites.py
Expand Up @@ -61,6 +61,8 @@ def register(self, model_or_iterable, admin_class=None, **options):
they'll be applied as options to the admin class. they'll be applied as options to the admin class.
If a model is already registered, this will raise AlreadyRegistered. If a model is already registered, this will raise AlreadyRegistered.
If a model is abstract, this will raise ImproperlyConfigured.
""" """
if not admin_class: if not admin_class:
admin_class = ModelAdmin admin_class = ModelAdmin
Expand All @@ -74,6 +76,10 @@ def register(self, model_or_iterable, admin_class=None, **options):
if isinstance(model_or_iterable, ModelBase): if isinstance(model_or_iterable, ModelBase):
model_or_iterable = [model_or_iterable] model_or_iterable = [model_or_iterable]
for model in model_or_iterable: for model in model_or_iterable:
if model._meta.abstract:
raise ImproperlyConfigured('The model %s is abstract, so it '
'cannot be registered with admin.' % model.__name__)

if model in self._registry: if model in self._registry:
raise AlreadyRegistered('The model %s is already registered' % model.__name__) raise AlreadyRegistered('The model %s is already registered' % model.__name__)


Expand Down
6 changes: 5 additions & 1 deletion tests/regressiontests/admin_registration/models.py
Expand Up @@ -7,5 +7,9 @@
class Person(models.Model): class Person(models.Model):
name = models.CharField(max_length=200) name = models.CharField(max_length=200)


class Place(models.Model): class Location(models.Model):
class Meta:
abstract = True

class Place(Location):
name = models.CharField(max_length=200) name = models.CharField(max_length=200)
11 changes: 9 additions & 2 deletions tests/regressiontests/admin_registration/tests.py
@@ -1,8 +1,8 @@
from django.test import TestCase from django.test import TestCase

from django.core.exceptions import ImproperlyConfigured
from django.contrib import admin from django.contrib import admin


from models import Person, Place from models import Person, Place, Location


class NameAdmin(admin.ModelAdmin): class NameAdmin(admin.ModelAdmin):
list_display = ['name'] list_display = ['name']
Expand Down Expand Up @@ -52,3 +52,10 @@ def test_iterable_registration(self):
isinstance(self.site._registry[Place], admin.options.ModelAdmin) isinstance(self.site._registry[Place], admin.options.ModelAdmin)
) )
self.assertEqual(self.site._registry[Place].search_fields, ['name']) self.assertEqual(self.site._registry[Place].search_fields, ['name'])

def test_abstract_model(self):
"""
Exception is raised when trying to register an abstract model.
Refs #12004.
"""
self.assertRaises(ImproperlyConfigured, self.site.register, Location)

0 comments on commit 12bd7bc

Please sign in to comment.