我正在尝试通过为书籍制作笔记应用程序来学习 Peewee 和 Bottle。
模型和关系由 Subject -< Book -< Chapter -< Note 组成.
关于note.tpl页面,我希望在表格中显示所有注释,并允许最终用户根据需要更新任何注释:
<FORM action="/notes" method="POST">
<input type="hidden" name="chapter_id" value="{{chapter.id}}"
<% note_ids = ""
for note in chapter.notes:
note_ids += note.id
end
note_ids = note_ids[:-1]
%>
<input type="hidden" name="note_ids" value="{{note_ids}}" />
<TABLE>
<TR><TD>Note Meta</TD><TD>Note</TD></TR>
% for note in chapter.notes:
<TR><TD>
Tag:<input type="text" value="note_tag#{{note.id}}" /><br />
Pg#:<input type="text" value="page_number#{{note.id}}" /><br />
</TD>
<TD> <textarea name="note#{{note.id}}" /><br />
</TR>
% end
</TABLE>
</FORM>
假设我有一个这样的 Controller :
@app.route('/notes/, method='POST')
def note_action():
for i in request.forms.get('note_ids').split(','):
note = Note(tag=request.forms.get('note_tag#' + i),
page_number=request.forms.get('page_number#' + i),
chapter=request.forms.get('chapter_id'))
note.save()
chapter = Chapter.select().where(Chapter.id==chapter_id)
return template('notes', chapter=chapter)
这很糟糕,原因有以下三个:
1) 它不进行批量更新,而是一次更新每个音符。我如何在 Peewee 中进行批量更新?
2) 它更新每一个笔记,甚至是用户没有编辑过的笔记。我如何才能确定哪些行已被编辑并仅更新这些行?
如果没有使用 JQuery(或使用程度非常有限),我应该如何解决这些问题?
请您参考如下方法:
关于您的第一个问题,迟到总比不到好:Peewee API 没有像 JDBC 的 java.sql.Statement 中那样的 addBatch() 方法。相反,您通过“with”开始一个事务并做很多工作,Python 处理事务的提交。 Peewee 作者的这篇文章说明了这种方法(不幸的是,该文章末尾关于批量加载数据的链接已损坏):
https://groups.google.com/forum/#!msg/peewee-orm/YWamQxAYxWU/HTu_wUNLHKUJ
