Python 属性错误:';功能';对象没有属性';地点id';
我试图做我们网站的用户关注学校功能,我制作了一个关系表(名为“关注”),如下所示Python 属性错误:';功能';对象没有属性';地点id';,python,flask-sqlalchemy,Python,Flask Sqlalchemy,我试图做我们网站的用户关注学校功能,我制作了一个关系表(名为“关注”),如下所示 class Follow(db.Model): __tablename__ = 'follow' follower_id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True) followed_id = db.Column(db.String(50), db.ForeignKey('school.
class Follow(db.Model):
__tablename__ = 'follow'
follower_id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True)
followed_id = db.Column(db.String(50),
db.ForeignKey('school.place_id'),primary_key=True)
timestamp = db.Column(db.DateTime, default=datetime.utcnow)
用户和学校模型的相关属性为:
class User(UserMixin, db.Model):
__tablename__ = 'user'
id = db.Column(db.Integer, primary_key=True)
followed = db.relationship('Follow',
foreign_keys=[Follow.follower_id],
backref=db.backref('follower', lazy='joined'),
lazy='dynamic',cascade='all,delete-orphan')
class School(db.Model):
_tablename_ = 'school'
place_id = db.Column(db.String(50), primary_key=True)
followers = db.relationship('Follow',
foreign_keys=[Follow.followed_id],
backref=db.backref('followed,lazy=joined'), # corresponding followed_id
lazy='dynamic',
cascade='all, delete-orphan')
此外,我还编写了三种与用户模型中的以下行为相关的方法
def follow(self, school):
if not self.is_following(school):
f = Follow(follower=self, followed=school)
db.session.add(f)
db.session.commit()
def unfollow(self, school):
f = self.followed.filter_by(followed_id=school.place_id).first()
if f is not None:
db.session.delete(f)
db.session.commit()
def is_following(self,school):
return self.followed.filter_by(followed_id=school.place_id).first() is not None
我还在main/views.py中写了以下内容
@main.route('/follow/<official_school_name>')
@login_required
# @permission_required(Permission.USER_LIKE)
def follow(official_school_name):
school = School.query.filter_by(official_school_name=official_school_name).first()
if school is None:
flash('Invalid school name.')
return redirect(url_for('.index'))
if current_user.is_following(school):
flash('You have already followed this school.')
return redirect(url_for('.school', official_school_name=school.official_school_name, roll_number=school.roll_number))
current_user.follow(user)
flash('You are not following %s.' % official_school_name)
return redirect(url_for('.school', official_school_name=school.official_school_name, roll_number=school.roll_number))
我尝试将“is_following”函数更改为:
def is_following(self,school):
return self.followed.filter_by(followed_id=school.id).first() is not None
然后出现了另一个错误:
File "C:\Users\kjhk\venv\lib\site-packages\flask\app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\kjhk\venv\lib\site-packages\werkzeug\contrib\fixers.py", line 152, in __call__
return self.app(environ, start_response)
File "C:\Users\kjhk\venv\lib\site-packages\flask\app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\kjhk\venv\lib\site-packages\flask\app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\kjhk\venv\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "C:\Users\kjhk\venv\lib\site-packages\flask\app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\kjhk\venv\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\kjhk\venv\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\kjhk\venv\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "C:\Users\kjhk\venv\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\kjhk\venv\lib\site-packages\flask\app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\kjhk\venv\lib\site-packages\flask_login\utils.py", line 261, in decorated_view
return func(*args, **kwargs)
File "E:\Project0619\ProjectTest\Code\app\main\views.py", line 194, in follow
if current_user.is_following(school):
File "E:\Project0619\ProjectTest\Code\app\models\User.py", line 113, in is_following
return self.followed.filter_by(followed_id=school.id).first() is not None
AttributeError: 'School' object has no attribute 'id'
这证明了第二个argus(School.id)可以引用“School”模型,但任何人都可以帮助我回答第一个错误出现的原因?非常感谢 此错误:
AttributeError:“School”对象没有属性“id”
不能很好地证明您的其他问题。仔细查看每个调用堆栈可以告诉我们更多关于正在发生的事情:
第一个调用使用如果不是self.is\u following(school)
作为其入口点,而第二个调用使用如果当前用户是following(school)
。我可以从你发布的追踪中看到这一点
在第二种情况下,您将学校定义为:
school = School.query.filter_by(official_school_name=official_school_name).first()
在后一种情况下,school
实际上是school
类的一个实例
但是,在第一个示例中,学校参数作为用户
传入:
current_user.follow(user)<-----
当前用户。跟随(用户)
school = School.query.filter_by(official_school_name=official_school_name).first()
current_user.follow(user)<-----