xubiaosunny
V2EX  ›  问与答

django jsonfield 中的内容是个列表,如何查询某个元素在里面

  •  
  •   xubiaosunny · Nov 6, 2020 · 1469 views
    This topic created in 2040 days ago, the information mentioned may be changed or developed.

    django3.1 所有数据库都支持 jsonfield 了。

    我有一个字段 tags 是 jsonfield,表内容大概这样 id tags 1 [1, 2] 2 [1, 3] 3 [2] 现在我想查询有标签 2 的所有数据,而且兼容 sqlite (__contains 等查询在 sqlite 的 jsonfeild 中不支持)

    这样查询是无效的 Model.object.filter(taags__has_key=2)

    这样是只能查出部分数据。Model.object.filter(taags__0=2) 或 Model.object.filter(taags__1=2)

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3194 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 12:28 · PVG 20:28 · LAX 05:28 · JFK 08:28
    ♥ Do have faith in what you're doing.