V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
honmaple
V2EX  ›  Python

关于 sqlalchemy 中 desc 的疑问

  •  
  •   honmaple · 2016-02-28 21:39:17 +08:00 · 1799 次点击
    这是一个创建于 2987 天前的主题,其中的信息可能已经有所发展或是发生改变。

    关于 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 条回复    2016-03-03 11:55:06 +08:00
    honmaple
        1
    honmaple  
    OP
       2016-03-03 11:55:06 +08:00
    设置默认排序使用
    __mapper_args__ = {
    "order_by":time.desc()
    }
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2319 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 04:05 · PVG 12:05 · LAX 21:05 · JFK 00:05
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.