如何通过HTML表单插入关联表?

时间:2017-03-16 17:13:26

标签: python flask sqlalchemy jinja2

尝试重现(以最简单的方式)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实例之间的新关联;
  • 或者,如果Tag不存在,则创建一个新的Tag实例(当然,UPDATE assoc表,以便Book实例与新Tag正确关联)

我觉得这个任务对我来说很复杂,因为我仍然在使用Jinja循环中的范围。但我知道,我需要做这样的事情:

  1. 抓取用户的输入;检查tag_name

  2. 中是否已存在__tablename__ = "tags"
  3. 如果tag_name已经存在,则获取其tag_id以及Book实例的book_id并将行添加到assoc表(即book_id) | TAG_ID)

  4. 如果tag_name不存在,请创建新的Tag()实例,然后执行第2步

1 个答案:

答案 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}}