python - 使用sqlalchemy.sql.functions.char_length作为过滤条件

标签 python sqlalchemy python-elixir

我正在使用 Elixir 和 sqla 0.6,我正在尝试查询我的模型:

class Document(Entity): 
    using_options(shortnames=True, order_by='doc_date')
    doc_number = Field(Unicode(20),index=True)

...对于具有给定长度数字的文档。

我在想这样的事情:

Document.query.filter(Document.doc_number.char_lenght()==5).all()

...但显然,char_length 虽然存在于 sqlalchemy.sql.functions 中,但在这里不起作用。我怎样才能让它在声明性习语中工作,而不求助于直接查询?

最佳答案

好的,找到答案了:

from sqlalchemy import func
Document.query.filter(func.char_length(Document.doc_number)==5).all()

关于python - 使用sqlalchemy.sql.functions.char_length作为过滤条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6600008/

相关文章:

python - Discord.py 消息被发送多次,每次增加 1

python - matplotlib 图例中的上标格式

python - 使用 selenium 在 python 中切换到弹出窗口

python - 我应该使用 SQLObject、SQLAlchemy 还是 SQLAlchemy + Elixir?

python - 将 PDT 中的时间列表转换为 python 中的 UTC

python - SQLalchemy 基于Mixin列的联合继承查询

python - SQLAlchemy 在查询中获取子字符串

python - 从 m2m 表中删除行时,sqlalchemy CompileError 未使用的列名

python - 如何创建非持久 Elixir/SQLAlchemy 对象?

python - 如何使用 Elixir/SQLAlchemy 进行原子递增/递减