[Tutor] Mapping to object attributes

Mike Hansen mhansen at cso.atmel.com
Tue Oct 31 23:29:32 CET 2006


I've got a web form with a lot of form fields. I'd like to be able to map
the form fields to an object's attributes. I'm having a little trouble
figuring out how.

I was thinking of something like this..

form = cgi.FieldStorage()
widget = Widget() 

form_field_object_map = {'widget_name' : widget.name, 
                         'alt_widget_name' : widget.alt_name,
	            		 ...}
			 
for form_field in form_field_object_map.keys():
    form_field_object_map[form_field]...er....
    
I'd like to avoid a pile if elif elifs. It seems like there should be a
cleaner way.

if form_field == 'widget_name':
    widget.widget_name = form.getvalue(form_field)
elif form_field == 'alt_widget_name':
    widget.alt_name = form.getvalue(form_field)
elif ...


There will be some fields I'll need to validate(boolean or int), but the
majority are just text fields that can be passed to the object.

Anyone got any ideas on how to do this?

Thanks,

Mike



More information about the Tutor mailing list