Как фильтровать + выбирать json внутри Jsonfield в django-rest-framwork

В одном столбце ответ хранится следующим образом :-

Теперь я хочу отфильтровать этот ответ

[
    {
        "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"
    }
]

До сих пор я пробовал django orm, но не смог получить ожидаемый результат

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.

Вернуться на верх