Monday 15 December 2008

Abstracting forms and models in appengine & django

If you have many Models in your appengine, django apps you are not going to create a separate form class for all the Models, are you? You got to be kidding me.
So, here is a simple short code that will generate the class object for you, just pass in various details :-)



def Formfactory(model, exclude_list, data=None, instance=None):
"""
An abstraction layer for forms and models
"""
class AbstractForm(djangoforms.ModelForm):
class Meta:
pass
setattr(Meta, 'model', model)
setattr(Meta, 'exclude', exclude_list)

myForm = AbstractForm(data=data, instance=instance)
return myForm

### Usage
t = Formfactory(Person, ['created_by', 'date_of_joining'] , data={'Name':'pranny'})


It can be normally used with all the is_valid() and rest.

Hope you will find it useful. Don't forget to comment.

1 comment:

  1. bro plz tell me how to get .name domain name
    plz reply me at umesh2020@yahoo.co.in
    i m thenkful to u
    btw nice blog

    ReplyDelete