如何在django-rest-framwork中过滤并选择Jsonfield中的json

qpgpyjmq  于 2023-01-31  发布在  Go
关注(0)|答案(1)|浏览(120)

在一列中,响应存储如下:
我有这个model.py:

class Form(models.Model):
    id = models.CharField(max_length=100, blank=True)
    children = models.JSONField(null=True, blank=True)

这是我的views.py文件:

class FormAPIView(APIView):
    def get(self, request):
        queryset = Form.objects.all()
        queryset = queryset.filter(children__contains=[{"id": "propertyName#0"}])

我已经尝试过了,但是没有得到预期的输出
现在我要过滤这个响应

[
    {
        "id": "A",
        "children": [
            {
                "id": "propertyName#0",
                "index": 0,
                "label": "Property",
            },
            {
                "id": "userName#0",
                "index": 1,
                "label": "Reported By",
            },
            {
                "id": "textinput#0",
                "index": 2,
                "label": "Reported By Title",
            },
            {
                "id": "dateinput",
                "index": 3,
                "label": "Date Reported",
            }
        ],
        "component": "sectionDivider"
    },
    {
        "id": "B",
        "children": [
            {
                "id": "propertyName#0",
                "index": 0,
                "label": "Property",
            },
            {
                "id": "userName#0",
                "index": 1,
                "label": "Reported By",
            },
            {
                "id": "textinput#0",
                "index": 2,
                "label": "Reported By Title",
            },
            {
                "id": "dateinput",
                "index": 3,
                "label": "Date Reported",
            }
        ],
        "component": "sectionDivider"
    },
    {
        "id": "C",
        "children": [
            {
                "id": "propertyName#0",
                "index": 0,
                "label": "Property",
            },
            {
                "id": "userName#0",
                "index": 1,
                "label": "Reported By",
            },
            {
                "id": "textinput#0",
                "index": 2,
                "label": "Reported By Title",
            },
            {
                "id": "dateinput",
                "index": 3,
                "label": "Date Reported",
            }
        ],
        "component": "sectionDivider"
    }
]

我想这样过滤我怎样才能得到这个响应
我有支票的ID,如ID:“A”,id:“B”应该只过滤A和B,在A和B内部我也想过滤。

[
    {
        "id": "A",
        "children": [
            {
                "id": "propertyName#0",
                "index": 0,
                "label": "Property",
            }
        ],
        "component": "sectionDivider"
    },
    {
        "id": "B",
        "children": [
            {
                "id": "propertyName#0",
                "index": 0,
                "label": "Property",
            },
            {
                "id": "userName#0",
                "index": 1,
                "label": "Reported By",
            }
        ],
        "component": "sectionDivider"
    }
]

到目前为止,我已经尝试djangoorm,但无法得到预期的输出

m1m5dgzv

m1m5dgzv1#

Django ORM确实提供了对JSONField内部嵌套查询的支持。
假设JSON字段定义如下:

class SomeModel(models.Model):
    ....
    parent = JSONField()

可以通过使用ORM和contains查找来实现所需的结果:

SomeModel.objects.filter(
    parent__contains=[{"id":"A", "children": [{"label": "Property"}]}, {"id":"B"}]
)

它非常强大,还允许你进行嵌套过滤,看看我是如何使用children:label过滤器和id过滤器的。

相关问题