Django ModelForm introspection

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
psykx
Posts: 408
Joined: Sat Feb 23, 2008 11:24 pm UTC
Location: England
Contact:

Django ModelForm introspection

Postby psykx » Fri Sep 21, 2012 10:40 am UTC

Hi all,
I'm trying to output generic HTML forms after parsing a Django model form object, but I'm getting lost in introspection. Here is what I have already

Code: Select all

#!/usr/bin/env python

import os
import sys
import argparse
import inspect

parser = argparse.ArgumentParser(description='Generate divs and css for django forms')
parser.add_argument('--path', help='path of models directory')

argNs = parser.parse_args()
args = argNs.__dict__

class formgen:
    def __init__(self, p):
        self.paths = []
        self.path = p
        sys.path.append(p)
       
    def getModelDirs(self):
        """ gets list of dirs containing models """
        dirList = os.listdir(self.path)
        for d in dirList:
            if os.path.isdir(self.path + d):
                #if directory has __init__.py then add to import list
                if os.access(self.path + d + '/__init__.py', os.F_OK):
                    self.paths.append(d)
        print self.paths
   
    def getModels(self):
        """ import models in the file at self.p """
        print self.p
        exec "from " + self.p + "import models as " + self.p + m 
   
    def getModelForms(self,Models):
        """ gets form models and stores data in dict"""
        for name, obj in inspect.getmembers(foo):
            if inspect.isclass(obj):
                print obj
   
    def generateForm():
        """ Generates html forms from dict """
        pass
   
    def run(self):
        self.getModelDirs()
        for p in self.paths:
            self.p = p
            self.getModels()
            #self.getModelForms()
            #self.generateForm()
   
generate = formgen(args['path'])
generate.run()



Can people see what the next step is?
Berengal wrote:Only if they're killer robots. Legos are happy robots. Besides, even if they were killer robots it wouldn't stop me. You can't stop science and all that.

D-503
Posts: 84
Joined: Sun Apr 15, 2012 11:35 pm UTC

Re: Django ModelForm introspection

Postby D-503 » Sat Sep 22, 2012 3:52 am UTC

I'm not sure what you're trying to achieve here, but might something like this work:
Django provides a helper class for creating a Form class from a Model.
see: https://docs.djangoproject.com/en/dev/t ... odelforms/
Once you have your form class you can just render it in a template with {{form.as_table}} to get your html string.

User avatar
psykx
Posts: 408
Joined: Sat Feb 23, 2008 11:24 pm UTC
Location: England
Contact:

Re: Django ModelForm introspection

Postby psykx » Sat Sep 22, 2012 9:26 am UTC

I did know that, but I hadn't considered it in this case. What I want is essentially an .as_div function. I'll probably write one and add it to the django git hub repository.
Berengal wrote:Only if they're killer robots. Legos are happy robots. Besides, even if they were killer robots it wouldn't stop me. You can't stop science and all that.

User avatar
psykx
Posts: 408
Joined: Sat Feb 23, 2008 11:24 pm UTC
Location: England
Contact:

Re: Django ModelForm introspection

Postby psykx » Sun Sep 23, 2012 12:01 pm UTC

Apparent the django developers don't want a as_div in the code base as they want to reduce html in the code base. A ludicrous idea IMHO as li and table is a far more specific html than divs but I digress. Essentially I want to write a script to generate the DIV html from ModelForms.
Berengal wrote:Only if they're killer robots. Legos are happy robots. Besides, even if they were killer robots it wouldn't stop me. You can't stop science and all that.


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 8 guests