Ticket #4326: django_new_admin_bug_fixed.patch

File django_new_admin_bug_fixed.patch, 3.2 KB (added by yi.codeplayer at gmail dot com, 18 years ago)

Add two things: 1. Make model registry OrderDict 2. use model.__name__ instead of model.__class__.__name__ when raise exception AlreadyRegistered because model is a class.

  • django/contrib/admin/sites.py

     
    22from django.contrib.admin import ModelAdmin
    33from django.contrib.auth import authenticate, login
    44from django.db.models import Model
     5from django.db.models.base import ModelBase
    56from django.shortcuts import render_to_response
    67from django.utils.text import capfirst
    78from django.utils.translation import gettext_lazy
     9from django.utils.datastructures import SortedDict
    810import base64
    911import cPickle as pickle
    1012import datetime
     
    5355
    5456class AdminSite(object):
    5557    def __init__(self):
    56         self._registry = {} # model_class class -> admin_class instance
     58        self._registry = SortedDict() # model_class class -> admin_class instance
    5759
    5860    def register(self, model_or_iterable, admin_class=None, **options):
    5961        """
     
    6971        """
    7072        admin_class = admin_class or ModelAdmin
    7173        # TODO: Handle options
    72         if issubclass(model_or_iterable, Model):
     74        if isinstance(model_or_iterable, ModelBase) and issubclass(model_or_iterable, Model):
    7375            model_or_iterable = [model_or_iterable]
    7476        for model in model_or_iterable:
    7577            if model in self._registry:
    76                 raise AlreadyRegistered('The model %s is already registered' % model.__class__.__name__)
     78                raise AlreadyRegistered('The model %s is already registered' % model.__name__)
     79            model._meta.admin = admin_class
    7780            self._registry[model] = admin_class(model)
    7881
    7982    def unregister(self, model_or_iterable):
     
    234237        Displays the main admin index page, which lists all of the installed
    235238        apps that have been registered in this site.
    236239        """
    237         app_list = []
     240        apps = SortedDict()
    238241        user = request.user
    239242        for model, model_admin in self._registry.items():
    240243            app_label = model._meta.app_label
     
    254257                        'admin_url': '%s/%s/' % (app_label, model.__name__.lower()),
    255258                        'perms': perms,
    256259                    }
    257                     app_list.append({
    258                         'name': app_label.title(),
    259                         'has_module_perms': has_module_perms,
    260                         'models': [model_dict],
    261                     })
     260                    if app_label in apps:
     261                        apps[app_label]['models'].append(model_dict)
     262                    else:
     263                        apps[app_label] = {
     264                            'name': app_label.title(),
     265                            # I found that this isn't used in template.
     266                            #'has_module_perms': has_module_perms,
     267                            'models': [model_dict],
     268                        }
    262269        return render_to_response('admin/index.html', {
    263270            'title': _('Site administration'),
    264             'app_list': app_list,
     271            'app_list': apps.values(),
    265272        }, context_instance=template.RequestContext(request))
    266273
    267274# This global object represents the default admin site, for the common case.
Back to Top