假设我有一个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
数据?