Ticket #12004: 12004_admin_abstract_models.2.diff

File 12004_admin_abstract_models.2.diff, 2.5 KB (added by Julien Phalip, 13 years ago)

Simplified test and move it to a better home (regressiontests/admin_registration/)

  • django/contrib/admin/sites.py

    diff --git a/django/contrib/admin/sites.py b/django/contrib/admin/sites.py
    index c1f6970..870ab04 100644
    a b class AdminSite(object):  
    6161        they'll be applied as options to the admin class.
    6262
    6363        If a model is already registered, this will raise AlreadyRegistered.
     64       
     65        If a model is abstract, this will raise ImproperlyConfigured.
    6466        """
    6567        if not admin_class:
    6668            admin_class = ModelAdmin
    class AdminSite(object):  
    7476        if isinstance(model_or_iterable, ModelBase):
    7577            model_or_iterable = [model_or_iterable]
    7678        for model in model_or_iterable:
     79            if model._meta.abstract:
     80                raise ImproperlyConfigured('The model %s is abstract and '
     81                      'therefore cannot be registered' % model.__name__)
     82
    7783            if model in self._registry:
    7884                raise AlreadyRegistered('The model %s is already registered' % model.__name__)
    7985
  • tests/regressiontests/admin_registration/models.py

    diff --git a/tests/regressiontests/admin_registration/models.py b/tests/regressiontests/admin_registration/models.py
    index 4a2d4e9..129530d 100644
    a b from django.db import models  
    77class Person(models.Model):
    88    name = models.CharField(max_length=200)
    99
    10 class Place(models.Model):
     10class Location(models.Model):
     11    class Meta:
     12        abstract = True
     13   
     14class Place(Location):
    1115    name = models.CharField(max_length=200)
  • tests/regressiontests/admin_registration/tests.py

    diff --git a/tests/regressiontests/admin_registration/tests.py b/tests/regressiontests/admin_registration/tests.py
    index e2a5d7e..92e9d8a 100644
    a b  
    11from django.test import TestCase
    2 
     2from django.core.exceptions import ImproperlyConfigured
    33from django.contrib import admin
    44
    5 from models import Person, Place
     5from models import Person, Place, Location
    66
    77class NameAdmin(admin.ModelAdmin):
    88    list_display = ['name']
    class TestRegistration(TestCase):  
    5252            isinstance(self.site._registry[Place], admin.options.ModelAdmin)
    5353        )
    5454        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)
Back to Top