[英]Python Flask SqlAlchemy Add Database Model Name dynamically in For Loop
I am not that familiar with Python and SQLAlchemy so please be patient:)我对 Python 和 SQLAlchemy 不太熟悉,所以请耐心等待:)
I need to capture if, within a FORM that holds multiple ICONS(files), one or more ICONS have been changed when editing the record.如果在包含多个图标(文件)的表单中,我需要捕获在编辑记录时是否更改了一个或多个图标。 To see which ICONS have been changed I created an Object holding the changes with "Database Model Name" as the "Key" and its "Value"
要查看哪些图标已更改,我创建了一个 Object 以“数据库 Model 名称”作为“键”及其“值”进行更改
{'icon': <FileStorage: 'fire.png' ('image/png')>}
key = used as database model name key = 用作数据库 model 名称
value = file.filename值= 文件.文件名
now when I try the get the data within a for loop and add this data to the Database model, nothing happens and it looks like I am not really accessing variable "k" in the loop.现在,当我尝试在 for 循环中获取数据并将此数据添加到数据库 model 时,没有任何反应,看起来我并没有真正访问循环中的变量“k”。
for k, v in notequalat.items():
responseteamdata.k = v.filename
My question is, how can I combine the Database model class " responseteamdata " and the variable " k " so that I can add the changes to the database model dynamically.我的问题是,如何将数据库 model class“ responseteamdata ”和变量“ k ”结合起来,以便我可以动态地将更改添加到数据库 model。
here is the full code:这是完整的代码:
if not notequalat:
try:
responseteamdata.title = title
responseteamdata.abbreviation = abbreviation
responseteamdata.isfireteam = booleanisfireteam
responseteamdata.iconposition = newlatlng
db.session.commit()
except IntegrityError:
db.session.rollback()
db.session.close()
res = make_response(jsonify("message ", "Error Updating the Team"), 500)
return res
else:
responseteamdata.title = title
responseteamdata.abbreviation = abbreviation
responseteamdata.isfireteam = booleanisfireteam
responseteamdata.iconposition = newlatlng
for k, v in notequalat.items():
responseteamdata.k = v.filename
db.session.commit()
dbevent = "updated"
db.session.close()
To be able to dynamically assign the Table Column Name the following command has been working for me:为了能够动态分配表列名称,以下命令一直在为我工作:
setattr(DB-Object, ColumnName, Value)
which means in my case:这意味着在我的情况下:
setattr(responseteamdata, k, v.filename)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.