这是我的模特
class Monkey(db.Model):
__tablename__ = 'monkey'
__table_args__ = (
db.UniqueConstraint('best_friend'),
)
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(64), index=True, unique=True)
age = db.Column(db.Integer)
email = db.Column(db.String(128), index=True, unique=True)
best_friend = db.Column(db.Integer, db.ForeignKey('monkey.id'))
password = db.Column(db.String(128))
is_admin = db.Column(db.Boolean)
def __init__(self, name, age, email, password, best_friend=None, is_admin=False):
self.name = name
self.age = age
self.email = email
self.password = generate_password_hash(password)
self.best_friend = best_friend
self.is_admin = is_admin
def number_friends(self):
number = Friendship.query.filter_by(monkey=self.id).count()
return number
def get_best_friend(self):
bf = Monkey.query.filter_by(id=self.best_friend).first()
return bf.name
我正在尝试通过他们的朋友数量和他们最好的朋友的名字来查询我的猴子,所以我做了这样的查询:
Monkey.query.order_by(Monkey.number_friends()).paginate(page, 5, False)
所以这不适合按朋友数排序,我收到这个错误:
TypeError: number_friends() missing 1 required positional argument: 'self'