4

I am writing a Flask application with SQLalchemy and WTForms.

Trouble with database updates... I have many object table fields I am trying to update.

so I started with this...

# this works great but needs many lines of code
event.evt_num = form.evt_num.data
event.evt_name = form.evt_name.data
event.evt_location = form.evt_location.data
event.evt_address = form.evt_address.data
event.evt_city = form.evt_city.data
event.evt_state = form.evt_state.data
...

While the above snippet works, in reality it is huge. So I would like to be smarter and iterate thru the attributes to update the database. So I tried the following...

event_fields = ('evt_num', 'evt_name', 'evt_location', 'evt_address',
                    'evt_city', 'evt_state', 'evt_zip', 'evt_notes')

for aa in event_fields:
    print "form data", getattr(form, aa).data  # prints posted form data
    print "db data", getattr(event, aa) # prints posted data from database

    if getattr(form, aa).data != getattr(event, aa):  # if form data shows a change
        setattr(event, aa, getattr(form, aa).data)    # update database does not work
        db_change = True
        session.flush()
        ...

This line doesnt work to update the database:

setattr(event, aa, getattr(form, aa).data)

I get this error "AttributeError: 'Event' object has no attribute 'aa' "

Your help will be greatly appreciated.

marvcode
  • 65
  • 1
  • 5
  • What if you store the results of your `getattr()` calls in some variables? That way, you aren't repeatedly making those calls. If `event` has the attribute at first, but then suddenly doesn't have it, could you be clearing it from the object when you use the `getattr()` function? If so, storing the results in a variable should also solve that problem. – coralvanda May 22 '16 at 06:11
  • Thanks. I tried removing the getattr(form, aa).data from setattr() function and stored that much to a temp variable and it resolved the issue. Many thanks! – marvcode May 22 '16 at 19:11

2 Answers2

2

What if you restructured like this?

event_fields = ('evt_num', 'evt_name', 'evt_location', 'evt_address',
            'evt_city', 'evt_state', 'evt_zip', 'evt_notes')

for aa in event_fields:
    form_data = getattr(form, aa).data
    db_data = getattr(event,aa)
    print "form data", form_data  # prints posted form data
    print "db data", db_data # prints posted data from database

    if form_data != db_data:  # if form data shows a change
        setattr(event, aa, form_data)    # update database now works
        db_change = True
        session.flush()
        ...

This way, you make fewer calls to getattr() and maybe optimize your code slightly.

coralvanda
  • 6,431
  • 2
  • 15
  • 25
0

Per user:coralv's comment, I resolved the problem by storing the getattr contents into a variable instead of having it as a part of the setattr function. Code ends up like this...

event_fields = ('evt_num', 'evt_name', 'evt_location', 'evt_address',
                'evt_city', 'evt_state', 'evt_zip', 'evt_notes')

for aa in event_fields:
    print "form data", getattr(form, aa).data  # prints posted form data
    print "db data", getattr(event, aa) # prints posted data from database
    if getattr(form, aa).data != getattr(event, aa):  # if form data shows a change
        val = getattr(form, aa).data
        setattr(event, aa, val)    # update database now works
        db_change = True
        session.flush()
        ...
marvcode
  • 65
  • 1
  • 5