Django

Code

Changeset 8063

Show
Ignore:
Timestamp:
07/23/08 13:58:06 (1 year ago)
Author:
jacob
Message:

Improved admin model registration options: you can now register using register(Model, **options) and even register(Model, ModelAdmin?, **options). This isn't documented yet -- a much expanded version of docs/admin.txt is on the way.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/contrib/admin/sites.py

    r8006 r8063  
    22from django.contrib.admin import ModelAdmin 
    33from django.contrib.auth import authenticate, login 
     4from django.core.exceptions import ImproperlyConfigured 
    45from django.db.models.base import ModelBase 
    56from django.shortcuts import render_to_response 
     
    6768        If a model is already registered, this will raise AlreadyRegistered. 
    6869        """ 
    69         do_validate = admin_class and settings.DEBUG 
    70         if do_validate: 
    71             # don't import the humongous validation code unless required 
     70        # Don't import the humongous validation code unless required 
     71        if admin_class and settings.DEBUG: 
    7272            from django.contrib.admin.validation import validate 
    73         admin_class = admin_class or ModelAdmin 
    74         # TODO: Handle options 
     73        else: 
     74            validate = lambda model, adminclass: None 
     75 
     76        if not admin_class: 
     77            admin_class = ModelAdmin 
    7578        if isinstance(model_or_iterable, ModelBase): 
    7679            model_or_iterable = [model_or_iterable] 
     
    7881            if model in self._registry: 
    7982                raise AlreadyRegistered('The model %s is already registered' % model.__name__) 
    80             if do_validate: 
    81                 validate(admin_class, model) 
     83 
     84            # If we got **options then dynamically construct a subclass of 
     85            # admin_class with those **options. 
     86            if options: 
     87                # For reasons I don't quite understand, without a __module__ 
     88                # the created class appears to "live" in the wrong place, 
     89                # which causes issues later on. 
     90                options['__module__'] = __name__ 
     91                admin_class = type("%sAdmin" % model.__name__, (admin_class,), options) 
     92 
     93            # Validate (which might be a no-op) 
     94            validate(admin_class, model) 
     95 
     96            # Instantiate the admin class to save in the registry 
    8297            self._registry[model] = admin_class(model, self) 
    8398