Login

Formalchemy hack for newforms-based form validation

Author:
erob
Posted:
January 19, 2009
Language:
Python
Version:
1.0
Tags:
newforms hack formalchemy notmm sqlalchemy
Score:
0 (after 0 ratings)

Very simple proof-of-concept that uses the django.forms library (newforms) for validation, and formalchemy for saving the model instance using sqlalchemy.

it can be used like this (pseudo-code):

if form.is_valid(): form.save(session=Session, app_label='Contact')

Feel free to improve the concept. Ideally, either use formalchemy or django.forms but not both like this example. ;-)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class ContactForm(forms.Form):

    def save(self, session=None, app_label=None):
        """ Save or update a new `Contact` """    
        # create this composite entity
        _data = {}
        for key,val in self.cleaned_data.items():
            _data[app_label+'--%s' % key] = val
        fs = FieldSet(Contact, session=session, data=_data)
        if fs.validate():
            fs.sync()
        return None

More like this

  1. newforms: Add field-specific error in form.clean() by miracle2k 7 years, 7 months ago
  2. create_object and update_object for newforms by danjak 7 years, 11 months ago
  3. Concepts in Django (interfaces) by douwevandermeij 1 year, 2 months ago
  4. unique validation for ModelForm by whiskybar 6 years, 11 months ago
  5. create_update for newforms (ModelForm) by Rozza 6 years, 11 months ago

Comments

erob (on January 19, 2009):

first snippet :-)

#

Please login first before commenting.