0

app.models に次の 2 つのクラスがあり、wagtail API を使用してデータを json として取得しています

class States(Page):
    name=models.CharField(max_length=50)

class Cities(Page):
    def get_state_name(self):
        return self.state.name
    name = models.CharField(max_length=50)
    state = models.ForeignKey(States, related_name='related_cities' )
    state_name = property(get_state_name)

したがって、/api/v1/pages/?type=dashboard.States&fields=name,related_cities を試すと、次のデータが返されます。

{
    "meta": {
        "total_count": 1
    },
    "pages": [
        {
            "id": 1,
            "name": "State1",
            "meta": {
                "type": "dashboard.States",
                "detail_url": "http://localhost:8000/api/v1/pages/2601/"
            },
            "related_cities": [
                {
                    "id": 28,
                    "meta": {
                        "type": "dashboard.Cities",
                        "detail_url": "http://localhost:8000/api/v1/pages/28/"
                    }
                },
                {
                    "id": 37,
                    "meta": {
                        "type": "dashboard.Cities",
                        "detail_url": "http://localhost:8000/api/v1/pages/37/"
                    }
                },
            ]
        }
    ]
}

related_cities フィールドには、都市のidとが返されます。meta追加のクエリを作成せずに、ここの応答で都市の名前を取得するにはどうすればよいですか? :/

Documentationに解決策が見つかりませんでした。何か不足していますか?私はこのような応答が欲しい

            "related_cities": [
                {
                    "id": 28,
                    "name": "SomeCityName1",
                    "meta": {
                        "type": "dashboard.Cities",
                        "detail_url": "http://localhost:8000/api/v1/pages/28/"
                    }
                },
                {
                    "id": 37,
                    "name": "SomeCityName2",
                    "meta": {
                        "type": "dashboard.Cities",
                        "detail_url": "http://localhost:8000/api/v1/pages/37/"
                    }
                },
            ]
4

1 に答える 1