Ticket #12004: 12004_admin_abstract_models.diff

File 12004_admin_abstract_models.diff, 3.5 KB (added by julien, 4 years ago)

Patch with correct diff extension

  • 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_views/models.py

    diff --git a/tests/regressiontests/admin_views/models.py b/tests/regressiontests/admin_views/models.py
    index 9ac99cc..bd3b956 100644
    a b class CyclicTwo(models.Model): 
    641641class Topping(models.Model):
    642642    name = models.CharField(max_length=20)
    643643
    644 class Pizza(models.Model):
     644class AbstractPizza(models.Model):
    645645    name = models.CharField(max_length=20)
     646
     647    class Meta:
     648        abstract = True
     649
     650class Pizza(AbstractPizza):
    646651    toppings = models.ManyToManyField('Topping')
    647652
    648653class PizzaAdmin(admin.ModelAdmin):
  • tests/regressiontests/admin_views/tests.py

    diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py
    index bbef907..a5cf305 100644
    a b import urlparse 
    66
    77from django.conf import settings
    88from django.core import mail
    9 from django.core.exceptions import SuspiciousOperation
     9from django.core.exceptions import SuspiciousOperation, ImproperlyConfigured
    1010from django.core.files import temp as tempfile
    1111from django.core.urlresolvers import reverse
    1212# Register auth models with the admin.
    from models import (Article, BarAccount, CustomArticle, EmptyModel, 
    3636    Language, Collector, Widget, Grommet, DooHickey, FancyDoodad, Whatsit,
    3737    Category, Post, Plot, FunkyTag, Chapter, Book, Promo, WorkHour, Employee,
    3838    Question, Answer, Inquisition, Actor, FoodDelivery,
    39     RowLevelChangePermissionModel, Paper, CoverLetter)
     39    RowLevelChangePermissionModel, Paper, CoverLetter, AbstractPizza)
    4040
    4141
    4242class AdminViewBasicTest(TestCase):
    class CustomModelAdminTest(AdminViewBasicTest): 
    530530        response = self.client.get('/test_admin/%s/my_view/' % self.urlbit)
    531531        self.assert_(response.content == "Django is a magical pony!", response.content)
    532532
     533class AdminRegistrationTest(TestCase):
     534
     535    def testAbstractModel(self):
     536        """
     537        Exception is raised when trying to register an abstract model.
     538        Refs #12004.
     539        """
     540        from django.contrib.admin import site
     541        # Smoke test to determine whether admin site works at all
     542        response = self.client.get('/test_admin/admin/admin_views/pizza/')
     543        self.failUnlessEqual(response.status_code, 200)
     544        self.assertRaises(ImproperlyConfigured, site.register, AbstractPizza)
     545
    533546def get_perm(Model, perm):
    534547    """Return the permission object, for the Model"""
    535548    ct = ContentType.objects.get_for_model(Model)
Back to Top