RSS

Blog

Django: Dynamically register Admin models

h311 May 2011 –  Comments (14)

Comments

Second version looks like dirty hack and isn't flexible enough. At least there should be added exclusion list similar to MIDDLEWARE_CLASSES of first version.

[] maxk ~ 3 years, 5 months ago at 9:44 a.m.

What is the purpose of: type(model, (admin.ModelAdmin,)) ? admin.site.register works perfectly fine with just the model as argument (and defaults to ModelAdmin for the Admin class)

[] Ulrich Petri ~ 3 years, 5 months ago at 1:29 p.m.

@Ulrich Petri

You're right, it's not necessary. I've let it there anyway because it's necessary if you want to set some default properties for the admin classes..

For example:

type(model, (admin.ModelAdmin,), {
    'list_display': ('title',  'description')
})

[] h3 ~ 3 years, 5 months ago at 8:26 p.m.

Combined a few of the ideas here into this.

from django.contrib import admin
from django.db.models import get_models, get_app, get_model
from myapp.models import *

APP_NAME = 'myapp'
APP_MODELS = [] # Use for specific models
# app_models = [get_model(APP_NAME, m) for m in APP_MODELS] # Get specified app models.
SEARCH_FIELDS = ('name', 'description',) # Add fields useful to search
app_models = get_models(get_app(APP_NAME)) # Get all app models.
for model in app_models:
    options = {}
    # Add search for some common fields.
    options['search_fields'] = [f for f in SEARCH_FIELDS if f in model._meta.get_all_field_names()]
    try:
        admin.site.register(model, type(model._meta.object_name, (admin.ModelAdmin,), options))
    except admin.sites.AlreadyRegistered:
        pass

[] Richard House ~ 3 years, 5 months ago at 7:46 a.m.

Even so, as a way to make that decion, you will probably first want to know, amongst other items, simply how much would a hair extensions transplant cost you.A symptom of hair extensions uk loss is fragile hair, so you need to keep your hair free from chemicals or high heat. Also, you should trim your Human Hair Weave often, and use styling products sparingly. Try to cut your hair extensions where you are able to wear it down.

[] Valerie Linderman ~ 2 years, 1 month ago at 3:44 a.m.

I'm currently working on a middleware that will act as a bridge between a website and a proprietary system.192.168.1.1

[] ipgood ~ 1 year, 5 months ago at 5:26 a.m.

That's how we feel about http://www.sugarsit.com coach handbags outlet by Steven http://www.legacyoutlet.com coach outlet store online shoes. However, the two-and-a-half http://www.chanluustores.com/ hour telecast http://www.ray-banitaly.com/ Ray ban can easily be summed up with these 12 coach outlet. And in a tiny tanktop no less. In addition to his http://www.coachstoreonlinebags.com Vanguard and Best Marc Jacobs Moonmen, http://www.legacyoutlet.com/limited-inventory-coach-outlet-stores-online-sale/ also took home awards for http://www.cocoachfactory.com/ and Best Editing.

[] coach handbags outlet ~ 1 year, 1 month ago at 9:54 p.m.

Fireworks,http://www.mmonsterheadphones.net/, these two words,http://www.guccishoesofactory.com/, let the listeners to look like one, pragmatic appropriateness,Coach Outlet Online, earthly warmth, used to describe the hometown of fireworks town, is the most relevant,Michael Kors States, home town, is a large town in west sichuan plain,North Faces Outlet, there are more than 2000 historical records,North Clearance Outlet Online, is the ancient chinese filial piety, is one big dutiful son han jiang shi's hometown,Louis Vuitton Shop, the legendary  one filial piety, and  ann send rice,http://www.northefaceoutletonline.net/, story took place here,Michael Kors Outlet, small town,Coach Factory Online, the streets were herringbone, play three angles,Michael Kors Outlet USA, triangles Xiaobazi,Gucci Shoes Factory, grocery shopping place,Coach Factory Outlet, thus,Monster Headphones Outlet, along the narrow,Louis Vuitton Shoes Factory, and winding streets, through the mill,http://www.laurenpoloralph.com/, post office, town, school, studio, theater, red shops, to commune hospitals,Coach Bags Outlet, streets around the bifurcation,Polo Outlet Online, a bonus street,MCM Outlet, a side street inhabited by the hui people,Coach Bags Store Online, all through the end of the rice market to end dam.

[] coachbag ~ 1 year ago at 5 a.m.

Wow, marvelous weblog layout! How long have you been running a blog for? you made running a blog look easy. The full look of your site is excellent, let alone the content!

[] make money online ~ 11 months ago at 9:35 a.m.

The full look of your site is excellent Z Code System

[] Genodif ~ 6 months, 2 weeks ago at 9:35 a.m.

To accommodate the data coming from this system I had to create REBEL MOUSE FAMILY SELF DEFENCE

[] Monica ~ 1 month ago at 7:48 a.m.

Copyrighted stuff .. u know.