在一列中,响应存储如下:
我有这个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,但无法得到预期的输出
1条答案
按热度按时间m1m5dgzv1#
Django ORM确实提供了对
JSONField
内部嵌套查询的支持。假设JSON字段定义如下:
可以通过使用ORM和contains查找来实现所需的结果:
它非常强大,还允许你进行嵌套过滤,看看我是如何使用
children:label
过滤器和id
过滤器的。