Is there a way to document the composition of an object required in a POST request using Django Rest Swagger?

95 Views Asked by At

Let's say I have an endpoint for creating Player that requires a Team object:

models.py

class Team(Model):
    name = CharField()

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

And I have a ModelViewSet as follows:

views.py

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

serializers.py

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

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

When I look at my Django Rest Swagger, the "Example value" for data I need to POST to create a player looks like:

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

What I want is for the {} to be expanded, to show that I would need to provide a {"name": "string"} object to create the Team object. I've tried adding

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

to the PlayerSerializer but that only adds the data to the Model tab on Django Rest Swagger. Is there a way to add this documentation automatically to the Example tab and Model showing the required POST data?

0

There are 0 best solutions below