auto generate admin.py

 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
#-----------------------------------------------------------------------------
# Name:        cvt.py
# Purpose:     auto generate django 1.0 admin.py from 0.9.6 models.py
#
# Author:      Hank Hu<hankhu2001@gmail.com>
#
# Created:     2008/08/30
# Copyright:   (c) 2006
# Licence:     Public
#-----------------------------------------------------------------------------
import sys
assert len(sys.argv) == 3, "usage: cvt prjname appname"
prjname, appname = sys.argv[1:]

import os
os.environ['DJANGO_SETTINGS_MODULE'] = prjname + '.settings'

exec "from %s.%s import models" % (prjname,appname)

fp = open("%s/%s/admin.py" % (prjname,appname), "w")

print >> fp,  '''#coding: utf-8
from django.contrib import admin
from %s.%s.models import *
''' % (prjname,appname)

for name, klazz in models.__dict__.items():
    if isinstance(klazz, type) and issubclass(klazz, models.models.Model):
        #print name, klazz
        if hasattr(klazz, 'Admin'):
            print >> fp,  'class %sAdmin(admin.ModelAdmin):' % name
            #print dir(klazz.Admin)
            attrs = [(k, v) for k, v in klazz.Admin.__dict__.items() if k[0] != '_']
            if len(attrs):
                for k, v in attrs:
                    if k=='fields':
                        k = 'fieldsets'
                    print >> fp, "\t%s=%s" % (k, repr(v).encode('utf-8'))
            else:
                print >> fp, "\tpass"
            print >> fp, "admin.site.register(%s, %sAdmin)" % (name, name)
            print >> fp

More like this

  1. Python Calendar wrapper template tag by dokterbob 4 years, 11 months ago
  2. Django code generator by jansta 3 years, 7 months ago
  3. django app name & breadcrumbs l10n by xormag 2 years, 9 months ago
  4. Functional Filters by waterson 6 years, 6 months ago
  5. Generic admin action export selected rows to excel by aemb87 1 year, 6 months ago

Comments

twinsant (on July 31, 2009):

good work. thank you!

#

(Forgotten your password?)