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
we21x
V2EX  ›  Python

如何在 tortoise-orm 的 JSON 字段中查找含有指定值的项?

  •  
  •   we21x · 2021-04-06 13:04:59 +08:00 · 1781 次点击
    这是一个创建于 1109 天前的主题,其中的信息可能已经有所发展或是发生改变。

    比如现在有如下表:

    class Foo(models.Model):
        id = fields.IntField(pk=True)
        tags = fields.JSONField(default=['aa', 'bb', 'cc'])
    

    想查询 tags 内含 bb 的数据项,该怎么写呢? 官方文档里面有个filtering感觉比较合适,但是这样写:

    bb_tags = await Foo.filter(tags__in=['bb'])
    

    并找不到数据。 另一个思路是直接写原始的 SQL Query:

    async with in_transaction() as conn:
        r = await conn.execute_query('SELECT tags FROM Foo')
    

    然而我并不会写...

    jenlors
        1
    jenlors  
       2021-04-06 15:02:56 +08:00   ❤️ 1
    we21x
        2
    we21x  
    OP
       2021-04-06 21:16:17 +08:00
    @long2ice 感谢!
    we21x
        3
    we21x  
    OP
       2021-04-06 22:50:59 +08:00
    @long2ice 大佬能给个例子吗?我没有 SQL 基础调了好半天也没看懂
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2845 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 13:37 · PVG 21:37 · LAX 06:37 · JFK 09:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.