关于 desc 我见过两种用法,分别是 order_by(Questions.time.desc())和 order_by(desc(Questions.time)),请教这两种用法有什么区别?
现在我要设置关联表的排序,应该使用哪种方法:
tag_question = db.relationship('Tags',
secondary=tag_question,
backref=db.backref('questions',
lazy='dynamic',
order_by='desc(Questions.time)')
)
还是
tag_question = db.relationship('Tags',
secondary=tag_question,
backref=db.backref('questions',
lazy='dynamic',
order_by='Questions.time.desc()')
)
但实际上 desc 并没有起作用(是我的错觉吗)
如果我要设置默认排序
__mapper_args__ = {
"order_by":'Questions.time desc'
}
却只有这种方法勉强有效,原来的两种方法直接运行不了,而Questions.time desc
虽然可以运行,但是有错误提示
/venv/lib/python3.5/site-packages/sqlalchemy/sql/compiler.py:575: SAWarning: Can't resolve label reference 'Questions.time desc'; converting to text() (this warning may be suppressed after 10 occurrences)
util.ellipses_string(element.element))
求助大神
1
honmaple OP 设置默认排序使用
__mapper_args__ = { "order_by":time.desc() } |