Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Python CherryPy SQLAlchemy class - how to loop through form vars and assign to dat aobject

Posted on 2014-10-16
1
Medium Priority
?
276 Views
Last Modified: 2014-10-20
Hi there,

I am using CherryPy and SQLALchemy and I am trying to put some code together to loop through the posted form vars and assign their values to my data object.  There are a lot of cases where I want to do this and the data records have a LOT of fields.

So, if the class I am trying to assign the values to is User with Last_Name and First_Name columns, for example.

I can loop through the kwargs and get the form vars.  The posted form-vars names/id's correspond with the column names on my User object.

But I don't know how to code an assignment as an expression

This_Object = User()     ... gives me my data class/object

.. then I loop through the parms ...

for lcParm in kwargs:
 lcVal = kwargs.get(lcParm,'')

..so, I can loop through and pick up the values, but how do I code the assignment of the value to the data object field?

The name is in lcParm (eg; Last_Name) .. but how do I code This_Object.lcParm = lcVal

Please help!

Thx, Scott
0
Comment
Question by:srbarker8
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 25

Accepted Solution

by:
clockwatcher earned 2000 total points
ID: 40385894
You should be able to use the setattr function.
This_Object = User()
for lcParm in kwargs:
    lcVal = kwargs.get(lcParm,'')
    if hasattr(This_Object, lcParm) and lcParm in User.__mapper__.columns:
        setattr(This_Object, lcParm, lcVal)
session.add(This_Object)

Open in new window

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question