Django过滤器JSONField字典列表 [英] Django filter JSONField list of dicts

查看:739
本文介绍了Django过滤器JSONField字典列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用新的JSONField运行Django 1.9并具有以下测试模型:

  class Test(TimeStampedModel):
actions = JSONField()

让我们说操作JSONField看起来像这样:

  [
{
fixed_key_1: foo1,
fixed_key_2:{
random_key_1: bar1,
random_key_2: bar2,
}
},
{
fixed_key_1: foo2,
fixed_key_2:{
random_key_3: bar2,
random_key_4: bar3,
}
}
]

我希望能够为列表的每个项目过滤foo1和foo2键。
当我这样做时:

 >> Test.objects.filter(actions__1__fixed_key_1 = foo2)

该测试位于查询集中。但是当我这样做时:

 >> Test.objects.filter(actions__0__fixed_key_1 = foo2)

不是,这很有意义。我想做这样的事情:

 >> Test.objects.filter(actions__values__fixed_key_1 = foo2)

Or

 >> Test.objects.filter(actions__values__fixed_key_2__values__contains = bar3)

并将Test放入查询集中。 / p>

任何想法是否可以做到以及如何实现?

解决方案

如果您不想按字典数组中的某个字段过滤数据,则可以尝试以下查询:

 测试。 objects.filter(actions__contains = [{'fixed_key_1':'foo2'}])

它将列出所有 Test 对象,这些对象在 actions 字段中至少具有一个包含键 fixed_key_1 的对象code>值 foo2



它也应适用于嵌套查找,即使您不t知道实际的索引:

  Test(actions = [
{'fixed_key_1':'foo4','fixed_key_3' :[[
{'key1':'foo2'},
]}
})。save()

Test.objects.filter(actions__contains = [{ 'fixed_key_3':[{'key1':'foo2'}]}])

简单来说,包含将忽略其他所有内容。



不幸的是,如果嵌套元素是一个对象,则必须知道键名。在这种情况下,按值查找将不起作用。


I run Django 1.9 with the new JSONField and have the following Test model :

class Test(TimeStampedModel):
    actions = JSONField()

Let's say the action JSONField looks like this :

[
  {
    "fixed_key_1": "foo1",
    "fixed_key_2": {
      "random_key_1": "bar1",
      "random_key_2": "bar2",
    }
  },
  {
    "fixed_key_1": "foo2",
    "fixed_key_2": {
      "random_key_3": "bar2",
      "random_key_4": "bar3",
    }
  }
]

I want to be able to filter the foo1 and foo2 keys for every item of the list. When I do :

>>> Test.objects.filter(actions__1__fixed_key_1="foo2")

The Test is in the queryset. But when I do :

>>> Test.objects.filter(actions__0__fixed_key_1="foo2")

It isn't, which makes sense. I want to do something like :

>>> Test.objects.filter(actions__values__fixed_key_1="foo2")

Or

>>> Test.objects.filter(actions__values__fixed_key_2__values__contains="bar3")

And have the Test in the queryset.

Any idea if this can be done and how ?

解决方案

If you wan't to filter your data by one of fields in your array of dicts, you can try this query:

Test.objects.filter(actions__contains=[{'fixed_key_1': 'foo2'}])

It will list all Test objects that have at least one object in actions field that contains key fixed_key_1 of value foo2.

Also it should work for nested lookup, even if you don't know actual indexes:

Test(actions=[
    {'fixed_key_1': 'foo4', 'fixed_key_3': [
        {'key1': 'foo2'},
    ]}
}).save()

Test.objects.filter(actions__contains=[{'fixed_key_3': [{'key1': 'foo2'}]}])

In simple words, contains will ignore everything else.

Unfortunately, if nested element is an object, you must know key name. Lookup by value won't work in that case.

这篇关于Django过滤器JSONField字典列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆