Answer the question
In order to leave comments, you need to log in
How to output manytomany relation in django rest api?
Good evening, I have 2 models
class Person(models.Model):
name = models.CharField(max_length=50)
def __str__(self):
return self.name
class Film(models.Model):
name = models.CharField(max_length=50)
year = models.CharField(max_length=50, default=None)
caption = models.CharField(max_length=50, default=None)
person = models.ManyToManyField(Person)
def __str__(self):
return self.name
{
"data": {
"name": "Терминатор",
"person": [
{
"id": 1,
"name": "Актер1"
},
{
"id": 2,
"name": "Актер2"
}
]
}
}
class FilmSerializers(serializers.ModelSerializer):
class Meta:
model= Film
fields=('id','name')
class FilmDetailSerializers(serializers.ModelSerializer):
person = PersonSerializers(many=True)
class Meta:
model= Film
fields=('name','person')
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question