Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #12004 -- Improved error reporting when an abstract class is re…

…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...
commit 12bd7bcb35703c2d2d19f6b79140d727ddee6e5f 1 parent 7aa8491
Russell Keith-Magee authored February 23, 2011
6  django/contrib/admin/sites.py
@@ -61,6 +61,8 @@ def register(self, model_or_iterable, admin_class=None, **options):
61 61
         they'll be applied as options to the admin class.
62 62
 
63 63
         If a model is already registered, this will raise AlreadyRegistered.
  64
+
  65
+        If a model is abstract, this will raise ImproperlyConfigured.
64 66
         """
65 67
         if not admin_class:
66 68
             admin_class = ModelAdmin
@@ -74,6 +76,10 @@ def register(self, model_or_iterable, admin_class=None, **options):
74 76
         if isinstance(model_or_iterable, ModelBase):
75 77
             model_or_iterable = [model_or_iterable]
76 78
         for model in model_or_iterable:
  79
+            if model._meta.abstract:
  80
+                raise ImproperlyConfigured('The model %s is abstract, so it '
  81
+                      'cannot be registered with admin.' % model.__name__)
  82
+
77 83
             if model in self._registry:
78 84
                 raise AlreadyRegistered('The model %s is already registered' % model.__name__)
79 85
 
6  tests/regressiontests/admin_registration/models.py
@@ -7,5 +7,9 @@
7 7
 class Person(models.Model):
8 8
     name = models.CharField(max_length=200)
9 9
 
10  
-class Place(models.Model):
  10
+class Location(models.Model):
  11
+    class Meta:
  12
+        abstract = True
  13
+
  14
+class Place(Location):
11 15
     name = models.CharField(max_length=200)
11  tests/regressiontests/admin_registration/tests.py
... ...
@@ -1,8 +1,8 @@
1 1
 from django.test import TestCase
2  
-
  2
+from django.core.exceptions import ImproperlyConfigured
3 3
 from django.contrib import admin
4 4
 
5  
-from models import Person, Place
  5
+from models import Person, Place, Location
6 6
 
7 7
 class NameAdmin(admin.ModelAdmin):
8 8
     list_display = ['name']
@@ -52,3 +52,10 @@ def test_iterable_registration(self):
52 52
             isinstance(self.site._registry[Place], admin.options.ModelAdmin)
53 53
         )
54 54
         self.assertEqual(self.site._registry[Place].search_fields, ['name'])
  55
+
  56
+    def test_abstract_model(self):
  57
+        """
  58
+        Exception is raised when trying to register an abstract model.
  59
+        Refs #12004.
  60
+        """
  61
+        self.assertRaises(ImproperlyConfigured, self.site.register, Location)

0 notes on commit 12bd7bc

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