Django admin register all models in admin interface

Normally it is really annoying, that you have to name all the models that you want to add to your admin site in Django in admin.py So I just wrote a little script that imports all of them. With some checks. You just need to edit the imports and you will have all the models in your admin front-end.

from django.contrib import admin
from models import *

import models
import inspect
from django.db.models.base import ModelBase

from django.contrib.admin.sites import AlreadyRegistered


for name, obj in inspect.getmembers(models ):
    if inspect.isclass(obj):
        if isinstance(obj, ModelBase):
            if not obj._meta.abstract:
                try:
                    admin.site.register(obj)
                except AlreadyRegistered:
                    pass

 just change:


from models import *

import models

and 

inspect.getmembers(models )

and you are ready to go :)

1 comment:

Anonymous said...

Pretty great post. I simply stumbled upon your blog and wanted
to say that I have really loved surfing around your blog posts.
After all I'll be subscribing for your feed and I'm hoping you write again
very soon!

Here is my web blog ... adobe