gpt4 book ai didi

django postgres JSONField |查询以 checkin 值列表(包含)

转载 作者:行者123 更新时间:2023-11-29 12:25:36 25 4
gpt4 key购买 nike

django 1.9(使用 postgresdb)中,我们正在使用 JSONField(在模型中),其条目如下所示:

**Entry 1**(in a row of that table):
data: {
"key1": "value1",
"key2": "value2",
"key3": "value3",
"key4": "value4"
}

**Entry 2**(in a row of that table):
data: {
"key5": "value4",
"key6": "value2",
"key7": "value2",
"key8": "value4"
}

我想查询值,比如 data__value__contains='value4'

Django 支持“键”(has_key, has_any_keys, has_keys) 相关查询

查询上述需求的好方法是什么?

最佳答案

如何使用 HStoreField 而不是 JSONField,这样您就可以访问 values查找。

MyModel.objects.filter(data__values__contains='value4')

Django docs 中所述,JSON 字段只有这些查找:

Containment and key operations¶

JSONField shares lookups relating to containment and keys with HStoreField.

  • contains (accepts any JSON rather than just a dictionary of strings)
  • contained_by (accepts any JSON rather than just a dictionary of strings)
  • has_key
  • has_any_keys
  • has_keys

关于django postgres JSONField |查询以 checkin 值列表(包含),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40001981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com