AttributeError: 'list' object has no attribute '_sa_instance_state'
AttributeError: 'list' object has no attribute '_sa_instance_state'
我收到以下错误,但我不确定如何修复它。我觉得跟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'
model.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
我收到以下错误,但我不确定如何修复它。我觉得跟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'
model.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