Digital Solutions & Online Creative 

Alex runs a small digital creative business from an office in London. It's called Outside In Media.

About

SOYREX is a web development and design resource intended as a place for me to share tips and tricks relating to html, css, web design, web development and other internet and web topics. If you like what you read, leave a comment, or send an email. Also, check out my portfolio.

This form does not yet contain any fields.
    Search the Archives
    Currently Reading..
    • Blink: The Power of Thinking Without Thinking
      Blink: The Power of Thinking Without Thinking
      by Malcolm Gladwell

      Blink talks about flash cognition and sub-conscious cognitive activity.. awesome read!

    • Confessions of an Economic Hit Man: The Shocking Story of How America Really Took Over the World
      Confessions of an Economic Hit Man: The Shocking Story of How America Really Took Over the World
      by John Perkins

      Confessions of an Economic Hit Man - i knew the world was a big conspiracy.. but this is a gripping insight into how the world really works.

    Recommended Reading
    • Designing with Web Standards (Voices That Matter)
      Designing with Web Standards (Voices That Matter)
      by Jeffrey Zeldman, Ethan Marcotte
    • Web Standards Creativity: Innovations in Web Design with XHTML, CSS, & DOM Scripting: Innovations in Web Design with XHTML, CSS, and DOM Scripting
      Web Standards Creativity: Innovations in Web Design with XHTML, CSS, & DOM Scripting: Innovations in Web Design with XHTML, CSS, and DOM Scripting
      by C et al Adams
    • CSS Mastery: Advanced Web Standards Solutions
      CSS Mastery: Advanced Web Standards Solutions
      by Andy Budd, Cameron Moll, Simon Collison
    Recent Comments
    Wednesday
    16Sep2009

    Auto Scaffold Django Admin Registration

    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.

    PrintView Printer Friendly Version

    EmailEmail Article to Friend

    Reader Comments (1)

    this is cool script thats for sharing .

    February 13, 2010 | Unregistered CommenterPhp Scripts

    PostPost a New Comment

    Enter your information below to add a new comment.

    My response is on my own website »
    Author Email (optional):
    Author URL (optional):
    Post:
     
    Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>
    « What's missing from Desktop Twitter Clients? | Main | How to demo your web designs to your clients.. »