AttributeError:“函数”对象没有属性“ place_id”

时间:2018-06-29 02:46:37

标签: 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.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))

最后,我只是做了一个非正式的按钮来测试跟随功能。

 <a href="{{ url_for('.follow', official_school_name=school.official_school_name, roll_number=school.roll_number) }}"
        class = "btn btn-primary">Follow</a>

但是,当我单击“跟随”按钮时,它显示错误:

File "C:\Users\kjhk\venv\lib\site-packages\flask\app.py", line 2309, in __call__
Open an interactive python shell in this framereturn 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 "E:\Project0619\ProjectTest\Code\app\main\views.py", line 197, in follow
current_user.follow(user)
File "E:\Project0619\ProjectTest\Code\app\models\User.py", line 99, in follow
if not self.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.place_id).first() is not None
AttributeError: 'function' object has no attribute 'place_id'

我试图将'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”模型,但是任何人都可以帮助我回答为什么出现第一个错误?非常感谢!

1 个答案:

答案 0 :(得分:0)

此错误:AttributeError: 'School' object has no attribute 'id'并不能证明您的其他问题。仔细查看每个调用堆栈可以了解更多有关发生的情况:

第一个呼叫使用if not self.is_following(school)作为其入口点,而第二个呼叫使用if current_user.is_following(school)。我可以从您发布的追溯中看到这一点。

在第二个中,您将学校定义为:

school = School.query.filter_by(official_school_name=official_school_name).first()

在后一种情况下,school实际上是School类的实例。

但是,在第一个示例中,学校arg以user的形式传入:

current_user.follow(user)<-----

...和user在您的代码中显然定义为类型function。您可以通过打印或返回type(user)进行测试。

school传递给第一个电话,这应该会停止AttributeError: 'function' object has no attribute 'place_id'的问题。