Django Rest Api - ManyToManyField, Display 'title' instead of 'id' in the Exercises Array
HTTP 200 OK
Allow: GET, POST, PUT, DELETE, PATCH
Content-Type: application/json
Vary: Accept
[
{
"id": 1,
"title": "Push Workout Bjarred",
"description": "Kör Hårt!",
"exercises": [
3,
4,
5,
6,
9,
10
],
"cardio": [
4
]
},
{
"id": 2,
"title": "Pull Workout Loddekopinge",
"description": "",
"exercises": [
1,
2,
7,
8
],
"cardio": []
},
{
"id": 3,
"title": "CardioPass",
"description": "",
"exercises": [],
"cardio": [
2,
3,
4
]
}
]
Serializer (So I want to display the title and not the id for every exercise)
class WorkoutSerializer(serializers.ModelSerializer):
class Meta:
model = Workout
fields = ('id', 'title', 'description', 'exercises', 'cardio')
Here are the models, note that I want to display the exercise name for every exercise in the api array, I don't want the id! - That is passed right now! :)
class Bodypart(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name
class Exercise(models.Model):
name = models.CharField(max_length=40)
bodyparts = models.ManyToManyField(Bodypart, blank=True)
def __str__(self):
return self.name
class Cardio(models.Model):
name = models.CharField(max_length=40)
time = models.IntegerField(default=10)
def __str__(self):
return self.name
class Meta:
verbose_name_plural = 'cardio'
class Workout(models.Model):
title = models.CharField(max_length=120)
description = models.CharField(max_length=1000, blank=True)
exercises = models.ManyToManyField(Exercise, blank=True)
cardio = models.ManyToManyField(Cardio, blank=True)
def __str__(self):
return self.title
Try adding exercises field in serializer as below
exercises = serializers.SlugRelatedField(read_only=True, slug_field='title')
Create a serializer for the
Exercise
model with the name field:And add it to the Workout serializer:
You can get this by changing your
WorkoutSerializer
like thisYou will get the result json like this
More about django rest serializers see https://www.django-rest-framework.org/api-guide/relations/
I came across the same problem and now I know the answer:
It must be
exercise
andcardio
which are the exact fields in workout (rather thanexercises
andcardios
. So basically, the answer from Mwangi Kabiru above is correct except that slug_field must be "name" and not title because the fields in class Exercise and Cardio are name, not title.