1

假设我有一个Player需要创建Team对象的端点:

模型.py

class Team(Model):
    name = CharField()

class Player(Model):
    name = CharField()
    team = ForeignKey(Team)

我有一个 ModelViewSet 如下:

视图.py

class PlayerViewSet(ModelViewSet):
    model = Player
    serializer = PlayerSerializer

序列化程序.py

class TeamSerializer(ModelSerializer):
    class Meta:
        model = Team
        fields = "__all__"

class PlayerSerializer(ModelSerializer):
    class Meta:
        model = Player
        fields = "__all__"

当我查看我的 Django Rest Swagger 时,POST创建播放器所需的数据的“示例值”如下所示:

{
    "name": "string",
    "team": {}
}

我想要的是{}被扩展,以表明我需要提供一个{"name": "string"}对象来创建Team对象。我试过添加

team = TeamSerializer(help_text="{'name': 'string'}")

PlayerSerializer只是将数据添加到 Django Rest Swagger 上的选项Model卡。有没有办法将此文档自动添加到Example选项卡并Model显示所需的POST数据?

4

0 回答 0