get full detail inside of id in django rest framework many to many
# models.py
from django.db import models
class Author(models.Model):
name = models.CharField(max_length=100)
class Book(models.Model):
title = models.CharField(max_length=200)
authors = models.ManyToManyField(Author)
# serializers.py
from rest_framework import serializers
class AuthorSerializer(serializers.ModelSerializer):
class Meta:
model = Author
fields = '__all__'
class BookSerializer(serializers.ModelSerializer):
authors = AuthorSerializer(many=True, read_only=True)
class Meta:
model = Book
fields = '__all__'
# views.py
from rest_framework import generics
from .models import Book
from .serializers import BookSerializer
class BookDetailAPIView(generics.RetrieveAPIView):
queryset = Book.objects.all()
serializer_class = BookSerializer
lookup_field = 'id'
Now, when you access the endpoint for a specific book (e.g., /books/1/
), it will return the full details of that book, including the related authors.