Proudly ProcrasDonating

Technology Thoughts

Django Tricks, Part 1 – Adminizer

This post is the first in a series on Django tricks.

Since the admin app is one of the stars of Django, we’ll start with our admin module, Adminizer.

Automate Admin Model creation

Automate Admin Model creation

The point of Adminizer is to reduce all that fluff around admin classes. Rather than literally writing admin classes for every one of our models, we automatically create them.

Adminizer.Adminize() takes a model or list of models as input. For each model, Adminizer creates a ModelAdmin class and registers it with the admin app.

What if we want to provide custom admin options? I initially wrote this so that we define a field in our model, called admin_options, that is a dictionary of the desired admin model fields. I’ve since added detection of an inner class called Admin with fields that match the admin model fields. This is more readable than a dictionary, and seems to be where the admin app is headed.

Voila! I frequently add new models to new and existing apps; by automating the AdminModel creation, I get the Admin app literally for free.

Below are the relevant code snippets building out from the models to the Adminizer.

Proudly ProcrasDonating,

Lucy.

data.py

class MyModel(models.Model):     slug = models.SlugField();     name = models.CharField(max_length=200)     admin_options = {'prepopulated_fields': {"slug": ("name",)} }     #or     class Admin:         prepopulated_fields = {"slug": ("name",)} ... ALL_MODELS = [MyModel, ...]

models.py

from data import ALL_MODELS as DATA_MODELS from rank import ALL_MODELS as RANK_MODELS from fps_models import ALL_MODELS as FPS_MODELS from log import ALL_MODELS as LOG_MODELS from analytics import ALL_MODELS as ANALYTICS_MODELS from data import * from rank import * from fps_models import * from log import * from analytics import * ALL_MODELS = DATA_MODELS + RANK_MODELS + FPS_MODELS + LOG_MODELS + ANALYTICS_MODELS

__init__.py

from models import ALL_MODELS from lib.admins import Adminizer Adminizer.Adminize(ALL_MODELS)

lib/admins.py

from django.contrib import admin import settings class Adminizer(object):     @classmethod     def _model_admin_class(cls, options):         """         @summary: returns an admin.ModelAdmin class with the specified         ModelAdmin options set. see:             http://docs.djangoproject.com/en/dev/ref/contrib/admin/#modeladmin-options         @param options: dictionary of ModelAdmin options. key should be string             that corresponds to option name (eg, 'date_hierarchy'). value should be             intended value for field.         @return: sub-class of admin.ModelAdmin         """         class klass(admin.ModelAdmin):             pass         for field_name, field_value in options.items():             setattr(klass, field_name, field_value)         return klass         @classmethod     def Adminize(cls, klasses):         """         """         if not isinstance(klasses, (list, tuple)):             klasses = (klasses,)         for klass in klasses:             if hasattr(klass, 'admin_options'):                 options = klass.admin_options             elif hasattr(klass, 'Admin'):                 options = {}                 for k, v in klass.Admin.__dict__.items():                     if k[:2] != "__":                         options[k] = v             else:                 options = {}             admin_klass = cls._model_admin_class(options)             admin.site.register(klass, admin_klass)

8 Comments »

[...] Proudly ProcrasDonating Technology Thoughts «Our Django Library, Part 1 – Adminizer [...]

[...] started with automating Admin Model creation. This post describes how we automate the creation of form classes for models. Automatic Model Form [...]

[...] started with automating Admin Model creation, and continued with automating initialization and installation, model mixins and [...]

[...] ProcrasDonating Technology Thoughts «Django Tricks, Part 1 – Adminizer Django Tricks, Part 3 – Model [...]

[...] started with automating Admin Model creation, and continued with automating initialization and installation, model mixins and [...]

[...] Welcome to Proudly ProcrasDonating :-) Subscribe to my RSS feed and visit ProcrasDonate.comThis post is the seventh and last in a series on Django tricks that began with automating Admin Model creation. [...]

[...] started with automating Admin Model creation, and continued with automating initialization and installation, model mixins, forms and app [...]

[...] started with automating Admin Model creation. This post describes how we automate the creation of form classes for models. Automatic Model Form [...]


Your comment

HTML-Tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>