Since the admin structure in Django changed (1.0?) I've found myself constantly feeling cheated that i have to actually create classes to get my admin system working - granted you usually want to create custom admin features for a model, but when you're just starting out with a project, the last thing you want to be doing is building generic admin classes and registering them with the admin app.
So, to combat this annoyance, i wrote a simple python script that i keep on my path on my mac, it just generates a scaffold of the admin classes i need for a project. Pretty simple.
#!/usr/bin/python
import sys
import re
if len(sys.argv) != 2:
print "Usage: adminscaffold <input-file>"
sys.exit()
arg = sys.argv[1]
inf = "%s/models.py" % arg
f = open(inf)
classes = []
registers = []
for line in f.readlines():
if re.match('class .*?\(models.Model\):',line):
(c,j) = line.split('(',2)
(j,cname) = c.split(' ',2)
classes.append("""
class %sAdmin(admin.ModelAdmin):
pass
""" % (cname))
registers.append("""admin.site.register(%s, %sAdmin)
""" % (cname,cname))
print """from django.contrib import admin
from %s.models import *
""" % (arg)
print "".join(classes)
print "".join(registers)
It's ugly and hacky, but it does do the job. Try it out.. basic usage is just:
$ django-admin-scaffold.py <app-name>
You run this in the same directory as your manage.py script.. and just provide the name of the app. It spits the code out on STDOUT, so you'll need to pipe it into a file to use it.