我想为客户端分页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')
)
答案 0 :(得分:2)
paginate()
在BaseQuery
上定义,Query
是sqlalchemy
中flask-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()