Login

Bind Administration

Author:
ashcrow
Posted:
September 10, 2008
Language:
Python
Version:
1.0
Score:
0 (after 0 ratings)

Binds $Model to $ModelAdmin without having to specify each binding manually. The ModelAdmins must have the same name as the model (as well as same case) with Admin appended.

Example: from django.db import models

class SomeModel(models.Model):
    name = models.CharField(max_length=255)

class AnotherModel(models.Model):
    name = models.CharField(max_length=255)

# bind administration
bind_administration('myproject.myapp.models', 'myproject.myapp.admin')
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import logging

from django.contrib.admin import ModelAdmin
from django.contrib import admin
from django.db.models import Model


def bind_administration(model_module, admin_module):
    """
    Binds Models with ModelAdmins.

    model_module is the module name where our Models live.
    admin_module is the module name where our ModelAdmins live.

        >>> bind_administration('project.app.models', 'project.app.admin')
        >>>
    """
    # import both modules
    mimp = __import__(model_module, fromlist=model_module.split('.')[-1])
    imp = __import__(admin_module, fromlist=admin_module.split('.')[-1])
    # We will hold a nice mapping to return in case the caller wants to verify
    admin_mapping = {}
    # For each item in the admin import
    for item in dir(imp):
        # We try/except because issubclass will freak out if item isnt a class
        try:
            real_admin = getattr(imp, item)
            # If item is a subclass of ModelAdmin then do some real work
            if issubclass(real_admin, ModelAdmin):
                logging.debug('%s is a subclass of ModelAdmin' % real_admin)
                # Create a nice mapping
                model_class_name = item.replace('Admin', '')
                real_model = getattr(mimp, model_class_name)
                # Make sure it's actually a Model and not some other item
                if issubclass(real_model, Model):
                    logging.debug('%s is a subclass of Model' % real_model)
                    admin_class_name = item
                    admin_mapping[model_class_name] = real_admin
                    # And register with admin
                    admin.site.register(real_model, real_admin)
                    logging.info('Registered %s with %s' % (
                        model_class_name, admin_class_name))
        except TypeError, te:
            # Not a class, go ahead and pass
            logging.debug('%s is not a class' % item)

    return admin_mapping

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 2 months ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 1 week ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 9 months, 4 weeks ago
  5. Help text hyperlinks by sa2812 10 months, 3 weeks ago

Comments

Please login first before commenting.