SQL Alchemy AttributeError:“unicode”对象没有属性“\u sa\u instance\u state”

2024-04-25 15:30:10 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我一直在创建一个数据库,用户可以在一个网上购物网站最喜欢的项目。我似乎得到了这个错误,即使当我键入b.title和board他们都打印“u'董事会名称'”。 应该这样,当有人选中一个board复选框时,该服装与board的关系应该被添加到数据库中。你知道吗

<form action="" method="POST" name="boards_list">
          <table class="table is-striped">
            {% for board in boards %}
            <tr>
              <td>
                <label class="checkbox">
                  <input type="checkbox" name="board_titles" value="{{board.title}}"/>
                </label>
                <a href="#">{{board.title}}</a>
              </td>
            </tr>
            {% endfor %}
          </table>

          <div class="field is-grouped">
            <div class="control">
              <button type="submit" class="button is-small">Add To Favs</button>
            </div>
          </div>
        </form>

这是上面显示的Jinja2模板中的代码。下面是根目录中的代码:

if request.method =="POST":
            f_b_titles = request.form.getlist("board_titles")
            clothing = Clothing.query.filter_by(name = name).first()
            userid = int(current_user.id)
            clothingid = int(clothing.id)
            for b in boards:
                for board in f_b_titles:
                    if b.title == board:
                        fav_relationship = Favourites_relationship(user_id = userid, clothing_id = clothingid, favs_board = b.title)
                        db.session.add(fav_relationship)
                        db.session.commit()
                        flash('Added favourite to mood board!', 'success')

最后,db模型的代码:

class Favourites_relationship(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), unique=True, nullable=False)
clothing_id = db.Column(db.Integer, db.ForeignKey('clothing.id'),nullable=False)
board_title = db.Column(db.Unicode, db.ForeignKey('favourites_board.title'), nullable=False)

以及:

class Favourites_board(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100), nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
favourites_relationships = db.relationship('Favourites_relationship', backref='favs_board', lazy=True)

有关错误的详细信息:

return self.wsgi_app(environ, start_response)

File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/flask/app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/flask/app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/leawhitelaw/Desktop/online_shop_data/flaskshop/routes.py", line 97, in product_details
fav_relationship = Favourites_relationship(user_id = userid, clothing_id = clothingid, favs_board = b.title)

Tags: inpyboardidappdbtitlerequest
2条回答

试试看

fav_relationship = Favourites_relationship(user_id = userid, clothing_id = clothingid, favs_board = b)

基本上错误的意思是,您只能将Favourites_board实例(例如fb = Favourites_board())分配给关系。你知道吗

所以假设boards是一个Favourites_board实例列表,您只需将该列表中的一个board(b)分配给favs_board=...。你知道吗

board_title = db.Column(db.Unicode, db.ForeignKey('favourites_board.title'), nullable=False)

这条线错了

here

When using the Unicode type, it is only appropriate to pass Python unicode objects, and not plain str. If a plain str is passed under Python 2, a warning is emitted. If you notice your application emitting these warnings but you’re not sure of the source of them, the Python warnings filter, documented at http://docs.python.org/library/warnings.html, can be used to turn these warnings into exceptions which will illustrate a stack trace:

因为您传递的是表单中的原始值,这需要python对象。你知道吗

尝试使用:

db.字符串

你知道吗数据库文本你知道吗

或者db.DETEXT文件你知道吗

相关问题 更多 >