尝试重现(以最简单的方式)MaciejCegłowski的http://pinboard.in;而不是链接和标签,我有书籍和标签。每个Book
都可以使用任意数量的Tag
标记,Tag
与许多Book
相关联。
class Book(db.Model):
__tablename__ = 'books'
book_id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(120), unique=True)
auth = db.Column(db.String(120), unique=True)
comment = db.Column(db.String(120), unique=True)
date_read = db.Column(db.DateTime)
era = db.Column(db.String(36))
url = db.Column(db.String(120))
notable = db.Column(db.String(1))
tagged = db.relationship('Tag', secondary=assoc, backref=db.backref('thebooks',lazy='dynamic'))
class Tag(db.Model):
__tablename__ = 'tags'
tag_id = db.Column(db.Integer, primary_key=True)
tag_name = db.Column(db.String(120))
def construct_dict(query):
books_dict = {}
for each in query: # query is {<Book object>, <Tag object>} in the style of assoc table - therefore, must make a dictionary bc of the multiple tags per Book object
book_data = books_dict.setdefault(each[0].book_id, {'bookkey':each[0], 'tagkey':[]}) # query is a list like this {index-book_id, {<Book object>}, {<Tag object #1>, <Tag object #2>, ... }}
book_data['tagkey'].append(each[1])
return books_dict
@app.route('/query')
def query():
query = db.session.query(Book, Tag).outerjoin('tagged') # query to get all books and their tags
books_dict = construct_dict(query)
return render_template("query.html", query=query, books_dict=books_dict)
这是我开始有点迷失的地方;也就是说,在构建适当的逻辑来处理我正在尝试做的事情时,这将在下面详细描述。
{% for i in books_dict %}
<a href="{{books_dict[i].bookkey.url}}" target="_blank">{{books_dict[i].bookkey.title}}</a>
{% for i in books_dict[i].tagkey %} # tagkey is a list of Tag objects; for each Book's tagkey, print each Object's tag_name
<a href="/tag/{{i.tag_name}}" class="tag-link">{{i.tag_name}}</a>
{% endfor %}
<a href="" class="edit">edit</a> # eventually, edit link will display the form
<form method="add_tag_to_book">
{% for j in books_dict[i].tagkey %}
<input type="text" name="tag" value="{{j.tag_name}}" />
{% endfor %}
<input type="submit" value="save">
</form>
{% endfor %}
对于任何一本书,用户(现在只是我)应该能够:
UPDATE
assoc
表,以便创建Book实例和Tag实例之间的新关联; UPDATE
assoc
表,以便Book实例与新Tag正确关联)我觉得这个任务对我来说很复杂,因为我仍然在使用Jinja循环中的范围。但我知道,我需要做这样的事情:
抓取用户的输入;检查tag_name
__tablename__ = "tags"
如果tag_name
已经存在,则获取其tag_id
以及Book实例的book_id
并将行添加到assoc
表(即book_id) | TAG_ID)
如果tag_name
不存在,请创建新的Tag()
实例,然后执行第2步
答案 0 :(得分:2)
首先,构建book_dict
字典:
def construct_dict(query):
books_dict = {}
for each in query: # query is {<Book object>, <Tag object>} in the style of assoc table - therefore, must make a dictionary bc of the multiple tags per Book object
book_data = books_dict.setdefault(each[0].book_id, {'bookkey':each[0], 'tagkey':[]}) # query is a list like this {index-book_id, {<Book object>}, {<Tag object #1>, <Tag object #2>, ... }}
book_data['tagkey'].append(each[1])
return books_dict
@app.route('/query')
def query():
query = db.session.query(Book, Tag).outerjoin('tagged') # query to get all books and their tags
books_dict = construct_dict(query)
return render_template("query.html", query=query, books_dict=books_dict)
然后,除了在Book
中打印每个book_dict
实例(并列出图书的相关Tag
个对象)之外,我们还会为每个Book
实例创建一个表单。允许用户为Tag
:
Book
{% for i in books_dict %}
<a href="{{books_dict[i].bookkey.url}}">{{books_dict[i].bookkey.title}}</a>,
{{books_dict[i].bookkey.auth}}
{% for i in books_dict[i].tagkey %}
<a href="/tag/{{i.tag_name}}" class="tag-link">{{i.tag_name}}</a>
{% endfor %}
<form action="{{ url_for('add_tag_to_book') }}" method=post class="edit-form">
<input type="hidden" name="book_id" value={{books_dict[i].bookkey.book_id}} />
<input type="text" name="tag_name" value="" />
<input type="submit" value="save">
</form>
{% endfor %}
...可见<input>
将获取用户输入的值name="tag_name"
;提交表单时,将调用路由/add_tag_to_book
路由。从表单中,我们抓取book_id
(在表单中打印但不可见,即<input type="hidden" name="book_id" value={{books_dict[i].bookkey.book_id}} />
);我们还使用<input>
)获取name="tag_name"
元素的值:
@app.route('/add_tag_to_book', methods=['POST'])
def add_tag_to_book():
b = request.form['book_id']
t = request.form['tag_name']
接下来,我们应该检查用户提交的tag_name
是否已经是Tag()
;如果在None
表中找到tag_name not ,则Python返回Tag
;否则,它将返回Tag
对象tag_name=t
(即用户提交的tag_name
);如果tag_object == None
,我们需要使用用户提供的Tag()
创建tag_name
的新实例:
tag_object = Tag.query.filter_by(tag_name=t).first()
if tag_object == None:
new_tag = Tag(tag_name=t)
db.session.add(new_tag)
db.session.commit()
tag_object = Tag.query.filter_by(tag_name=t).first()
tag_object_id = tag_object.tag_id
此时,我们将有一个tag_object
(我们Tag
表中新创建的或之前的tag_id
我们可以抓取book_id
并将其插入到我们的关联表中,以及Book
对象的book_id
。接下来,我们创建一个数据库连接,插入tag_id
&amp; query
,提交数据库,然后将用户返回到 conn = db.session.connection()
ins = assoc.insert().values(book_id=b,tag_id=tag_object_id)
result = conn.execute(ins)
db.session.commit()
return redirect(url_for('query'))
页面:
@app.route('/add_tag_to_book')
将所有内容放在一起,完整的@app.route('/add_tag_to_book', methods=['POST'])
def add_tag_to_book():
b = request.form['book_id']
t = request.form['tag_name']
tag_object = Tag.query.filter_by(tag_name=t).first()
if tag_object == None:
new_tag = Tag(tag_name=t)
db.session.add(new_tag)
db.session.commit()
tag_object = Tag.query.filter_by(tag_name=t).first()
tag_object_id = tag_object.tag_id
conn = db.session.connection()
ins = assoc.insert().values(book_id=b,tag_id=tag_object_id)
result = conn.execute(ins)
db.session.commit()
return redirect(url_for('query'))
如下所示:
{{1}}