[Flask] PyExcel with Flask, updating a record

Jim Icenhower jicenhower at yahoo.com
Wed Sep 23 16:17:02 CEST 2015


Hi,I'm trying to use pyexcel to import a spreadsheet but I don't want to add existing records to the table. I'd either like to skip them or, preferably, update them.I'm currently using the save_book_to_database() method with an init function. I'd love to be able to return None from the init function and have save_book_to_database() skip the record but that doesn't seem to be working.
Do I just need to hand code record inserts and updates or is there a library to handle that more elegantly?
Any help will be greatly appreciated!
Here's a snippet:
@app.route("/importEvents", methods=['GET', 'POST'])
def doEventsImport():
    if request.method == 'POST':
        def init_func(row):
            # see if this is an existing event
            eventKey = row["EventKey"]
            foundEvent = db.session.query(Event).filter_by(EventKey=row['EventKey']).first()
            if foundEvent:
                print 'foundEvent:'
                print foundEvent
                return None

            event = Event(row['EventKey'], row['Name'], row['Description'], row['BeginDate'], row['EndDate'], row['RegBeginDate'], row['RegEndDate'], row['Location'], row['Address1'], row['Address2'], row['City'], row['State'], row['Zip'], row['URL'], row['Email'], row['Phone'])
            return event

        request.save_book_to_database(field_name='file', session=db.session,
                                      tables=[Event],
                                      initializers=[(init_func)])
        return "Saved"
 Jim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/flask/attachments/20150923/d51d4d5c/attachment.html>


More information about the Flask mailing list