推荐学习书目
› 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 · Apr 6, 2021 · 3373 views
    This topic created in 2001 days ago, the information mentioned may be changed or developed.

    比如现在有如下表:

    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  
       Apr 6, 2021   ❤️ 1
    we21x
        2
    we21x  
    OP
       Apr 6, 2021
    @long2ice 感谢!
    we21x
        3
    we21x  
    OP
       Apr 6, 2021
    @long2ice 大佬能给个例子吗?我没有 SQL 基础调了好半天也没看懂
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4852 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 05:51 · PVG 13:51 · LAX 22:51 · JFK 01:51
    ♥ Do have faith in what you're doing.