AttributeError:“列表”对象没有属性“_sa_instance_state”

2023-12-11

我收到以下错误,我不知道如何修复它。我认为这与is_bestfriend关系。

File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/usr/local/lib/python2.7/dist-packages/flask_login.py", line 758, in decorated_view
return func(*args, **kwargs)
File "/home/peg/flask-Alembic/app/layout/view.py", line 176, in bestFriend
u = g.user.be_bestfriend(user)
File "/home/peg/flask-Alembic/app/model.py", line 84, in be_bestfriend
self.is_bestfriend = [user]
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/attributes.py", line 220, in __set__
instance_dict(instance), value, None)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/attributes.py", line 780, in set
value = self.fire_replace_event(state, dict_, value, old, initiator)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/attributes.py", line 801, in fire_replace_event
value = fn(state, value, previous, initiator or self._replace_token)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/unitofwork.py", line 83, in set_
newvalue_state = attributes.instance_state(newvalue)
AttributeError: 'list' object has no attribute '_sa_instance_state'

模型.py:

friends = db.Table('friends',
db.Column('user_id', db.Integer, db.ForeignKey('users.id')),
db.Column('friend_id', db.Integer, db.ForeignKey('users.id'))
)

class Users(db.Model):
    id = db.Column(db.Integer, primary_key = True)
    name = db.Column(db.String(50), index=True)
    age= db.Column(db.Integer )
    email = db.Column(db.String(50),index=True, unique= True)
    bestfriend_id = db.Column(db.Integer, db.ForeignKey('users.id'))

    is_bestfriend = db.relationship( 'Users', uselist=False, remote_side=[id], post_update=True)

    is_friend = db.relationship('Users', #defining the relationship, Users is left side entity
        secondary = friends, #indecates association table
        primaryjoin = (friends.c.user_id == id), #condition linking the left side entity
        secondaryjoin = (friends.c.friend_id == id),#cond if link right.s ent. with assoc table
        backref = db.backref('friends', lazy = 'dynamic'),#how accessed from right
        lazy = 'dynamic'
    ) 

    def are_bestfriends(self, user):
        return self.is_bestfriend == user

    #best friends management
    def be_bestfriend(self, user):
        if not self.are_bestfriends(user):
            self.is_bestfriend = [user]
            user.is_bestfriend = [self]
            return self

view.py:

@layout.route('/bestFriend/<name>')
@login_required
def bestFriend(name):
    user = Users.query.filter_by(name = name).first()
    if user is None:
        flash('User %s not found.' % name)
        return redirect(url_for('index'))
    if user == g.user:
        flash('You can\'t Best Friend yourself!')
        return redirect(url_for('layout.user', page=1,sortby='normal'))
    u = g.user.be_bestfriend(user)
    if u is None:
        flash('Cannot be best Friend ' + name + '.')
        return redirect(url_for('layout.user', page=1,sortby='normal'))
    db.session.add(u)
    db.session.commit()
    flash('You are now BestFriend with ' + name + '!')
    return redirect(url_for('layout.user', page=1,sortby='normal'))

您正在将一个列表分配给一个关系,其中uselist=False。您应该设置单个模型实例,而不是包含它的列表。

def be_bestfriend(self, user):
    if not self.are_bestfriends(user):
        self.is_bestfriend = user
        user.is_bestfriend = self
        return self
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

AttributeError:“列表”对象没有属性“_sa_instance_state” 的相关文章

随机推荐

  • 没有 mod_rewrite 的 php Zend / MVC

    我在网上的许多博客中都看到过它 但我相信应该在这里讨论它 当我们有 PHP 中的 MVC 框架 我对 ZEND 感兴趣 但我们的主机不提供 mod rewrite 时我们该怎么办 有什么 捷径 吗 我们可以以任何方式转移控制权 以便在页面之
  • 在不编码的情况下设置 ASP.NET Core TagHelper 属性

    我想添加integrity属性到我的标签助手中的脚本标签 它包含一个 我不想编码的标志 这是我的标签助手 HtmlTargetElement Attributes script public class MyTagHelper TagHel
  • 任务集-python

    我有一台双四核机器 所以 我的CPU列表是0 7 我正在尝试从 python 运行任务集 mapping 2 2 2 2 2 for i in range 0 len mapping cmd taskset c str mapping r
  • 如何在 Python Turtle 中定位文本?

    例如 write First Last True align right 但具有特定的 x y 坐标 随着使用像这样的命令setposition aka goto setx or sety 在你呼叫之前定位你的乌龟write the mov
  • ggplot2 热图,带有范围值的颜色

    我想在 ggplot2 中制作热图 我的玩具数据和代码是 set seed 12345 dat lt data frame Row rep x LETTERS 1 5 times 10 Col rep x LETTERS 1 10 each
  • Flutter:obviousText,如何停止显示键入的字符

    当在使用 obliqueText true 的字段中输入文本时 每个输入的字符都会在转换为项目符号之前短暂显示 你如何阻止这种行为 这是fixed在网络 桌面上 但没有在移动设备上执行此操作的选项 您可以按照此建议创建自定义 TextEdi
  • 如何使用 jQuery 将 html 表格单元格更改为文本输入

    所以我有一个表格 如下所示 tbody thead tr th Date Registered th th Name th th Organisation th th Email th th Job Title th th LSA th t
  • 一旦我限制我的 Google API 密钥,它就不再起作用

    我有一个正在使用的 Google API 密钥 并且在 Google 地图上运行良好 我现在尝试在我的 Android 应用程序中使用 Google 地图距离矩阵 API 并且只有当我不将密钥限制在 Android 应用程序上时 我才能使其
  • WordPress 中的正则表达式错误分隔符

    我是正则表达式的新手 我有一个简单的疑问 我在 wordpress 中找到了这段代码 self preg replace wp admin i self 根据php net 上的文档 不允许作为分隔符 有人可以解释一下代码吗 你尝试过吗 从
  • 将文档字符串设置为 def 内的表达式

    我想设置func doc 作为表达式 within def def f My function help Set the docstring def g My function help An expression so not read
  • Azure DevOps:代表另一个用户创建评论

    我正在寻找一种代表另一个用户 模拟另一个用户 向工作项添加评论的方法 VssConnection connection new VssConnection new Uri url new VssClientCredentials WorkI
  • PHP - 关于将 reCAPTCHA 与 jQuery 结合使用的问题

    这是一个教程 说明如何将 jQuery 表单验证与 reCAPTCHA 结合起来 http snipplr com view 15563 jquery validating recaptcha with ajax 根据我的理解 上面的教程实
  • 如何排列图像 3x3?

    我有九张图像 如果我必须将它们排列为 3x3 这意味着 3 行和 3 列 最好的方法是什么 我应该使用 CSS 吗 3x3 图像网格 该 CSS 允许您 垂直居中图像 水平居中图像 允许各种尺寸的图像 小于网格尺寸的图像保持原始尺寸 不会出
  • WPF 数据模板教程

    我陷入了如何在 WPF 中使用 DataTamplete 的困境 有人知道一些教程或者有一系列教程来教如何使用它 我正在使用 MVVM 模式 所以如果你知道 mvvm 示例中的教程会更好 Tks 谷歌搜索 wpf datatempalte
  • jQuery“data”属性未经过 W3C 验证

    我想知道为什么这段 html 由于 data 属性而无法验证 XHTML 1 0 过渡 我已经四处寻找答案 并且我没有使用 jQuery 元数据插件 如果这很重要的话 有什么方法可以让它验证 或者转义它 这样验证器就看不到它吗 div Be
  • [Windows、Qt5、QMediaPlayer、QMediaPlaylist]:当前视频源更改时短暂的黑屏

    我正在使用 Qt5 QMediaPlayer 编写一个视频播放器 以随机播放一些视频 如下所示 int main int argc char argv QApplication a argc argv QMediaPlaylist play
  • 嵌入式Redis尝试连接真实Redis服务器,导致异常

    我正在尝试使用嵌入式 redisdev弹簧简介 我也有一个cloud连接到真实 Redis 的配置文件 但是 我的嵌入式 Redis 配置似乎尝试连接到真正的 Redis 这是我运行时遇到的错误dev轮廓 Caused by redis c
  • 复制字体时 CopyHere 无法按预期工作

    我制作了一个脚本 该脚本应该将一堆字体复制到 Windows 字体文件夹中 当我运行它时 我收到了我想要复制的文件名的输出 但没有复制任何内容 当我删除 For 循环并指定文件名时它会起作用 任何帮助表示赞赏 Const FONTS H14
  • Vue-router 无法捕获 webpack 模板中带有点的路由

    我从以下位置启动了我的应用程序网页包模板并添加了路线 edit filename to it 问题是 包含点的文件名由 Express 处理 而不是由我的 Vue 应用程序处理 换句话说 edit 123与路线匹配但是 edit 123 j
  • AttributeError:“列表”对象没有属性“_sa_instance_state”

    我收到以下错误 我不知道如何修复它 我认为这与is bestfriend关系 File usr local lib python2 7 dist packages flask app py line 1836 in call return