简体   繁体   English

Python flask,一种更新 SQLAlchemy 记录的更简洁方法

[英]Python flask, a cleaner way to update a SQLAlchemy record

is there any way to perform database update in flask better than this:有什么方法可以比这更好地在 flask 中执行数据库更新:

@author_blueprint.route("/updateAuthor/<int:author_id>", methods=["GET","POST"])
def updateAuthor(author_id):
    author = Author.query.get(author_id)
    form = AuthorForm(request.form, obj=author)
    if request.method == "POST" and form.validate_on_submit():
        author.firstName = form.firstName.data //line 6
        author.lastName = form.lastName.data //line 7
        author.university = form.university.data.name //line 8
        author.department = form.department.data.name //line 9
        author.email = form.email.data //line 10
        db.session.commit()
        flash("Updated author Successfully")
        return redirect(url_for("author.createAuthor"))
    authors = author.query.all()
    return render_template("author/author.html", title="authors", form=form, authors=authors)

my record is updated using this way but I done want to write all the fields one by one to do the update like in lines 6 to 10我的记录是用这种方式更新的,但我确实想一个一个地写下所有的字段来做更新,就像第 6 行到第 10 行一样

just warp it up with some function sa用一些 function sa 把它翘起来

def update(author, form):
    author.firstName = form.firstName.data
    author.lastName = form.lastName.data 
    author.university = form.university.data.name
    author.department = form.department.data.name
    author.email = form.email.data

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM