为什么我得到了仪表列表'对象没有属性' paginate' filter_by in Flask?

时间:2017-10-21 11:46:21

标签: flask flask-sqlalchemy

我想为客户端分页subscribed_users但我收到的错误是:

cl = Client.query.filter_by(family='John').first()
cl.subscribed_users.paginate()

AttributeError: 'InstrumentedList' object has no attribute 'paginate'

这是我的 models.py

subscribers = db.Table(
    'clients_subscribed',
    db.Column('client_id', db.Integer, db.ForeignKey('client.id', ondelete='CASCADE')),
    db.Column('user_id', db.Integer, db.ForeignKey('user.id', ondelete='CASCADE'))
)

class Client(db.Model):
    subscribed_users = db.relationship(
        'User',
        secondary=subscribers,
        backref=db.backref('user', passive_deletes=True, lazy='dynamic')
    )

1 个答案:

答案 0 :(得分:2)

paginate()BaseQuery上定义,Querysqlalchemyflask-sqlalchemy的子类。当您在subscribed_users模型上执行查询时,paginate可用但flask-sqlalchemy不是db.Table模型,因此您无法在其上调用paginate,因为它没有。 解决方案是将class Subscribers(db.Model): client_id = db.Column(db.Integer, db.ForeignKey('client.id'), primary_key=True)) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True)) class Client(db.Model): subscribed_users = db.relationship( ... secondary=Subscribers, ... 更改为类,如下所示:

#!/usr/bin/python3

import requests


class Script:
    def main(self):
        res = requests.put('http://www.google.co.jp') # get response code 405
        print('Content-type: text/html; charset=UTF-8\n')
        print(res.content)

if __name__ == '__main__':
    Script().main()