V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
xubiaosunny
V2EX  ›  问与答

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

  •  
  •   xubiaosunny · Nov 6, 2020 · 1360 views
    This topic created in 1998 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   ·   3360 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 00:34 · PVG 08:34 · LAX 17:34 · JFK 20:34
    ♥ Do have faith in what you're doing.