Question

I am trying to loop through a list of known elements so I can check if they were submitted properly, then do something with them. Problem is, my elements are strings, but the corresponding element object is needed to assign a value to a user object.

Should something else be where eval(element) is?

elements = ('first_name', 'last_name', 'email')

u = request.user

for element in elements:    
    if not element in form.errors:
        dajax.alert('alert: %s' % form.cleaned_data[element])
        u.eval(element) = form.cleaned_data[element]
        dajax.add_css_class('div #%s' % element , 'form-group has-success')
Was it helpful?

Solution

Use setattr like so:

setattr(u, element, form.cleaned_data[element])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top