How I can use Nested serializer in Django Rest Framework

Hi I'm creating a serializer where I wanna show user profile details and all his products from the product model but it's not working

class UserSerializer(serializers.ModelSerializer):

related_products = ProductSerializer(
    source="user.product_set.all", read_only=True, many=True

class Meta:
    model = User
    fields = [

class ProfileView(generics.RetrieveAPIView):
serializer_class = UserSerializer
permission_classes = [permissions.IsAuthenticated, onlyuser]

def retrieve(self, request, *args, **kwargs):
    serializer = self.serializer_class(request.user)
    return Response(, status=status.HTTP_200_OK)

I found a solution if its a fine approach you can use it in your project

class ProfileView(generics.RetrieveAPIView):
def get(self, request, *args, **kwargs):

    qs = Product.objects.filter(user=request.user)
    data = {
        "user_profile": UserSerializer(request.user).data,
        "user_products": ProductSerializer(
            qs, many=True, context={"request": request}
    return Response(data=data)

** This is the example of 4 steps nested serializers. with maximum functionality example with good practice. You can try this. **


from rest_framework import serializers
from django.core.validators import MaxValueValidator, MinValueValidator
from diseases.models import (Pest, PesteDoseConfiguration, Disease, DiseaseDoseConfiguration)
from crops.models import (CropStage, CropVariety, Crop)

from project.settings import MEDIA_URL

class PestCropResponseSerializer(serializers.ModelSerializer):  

    class Meta:
        model = Crop
        fields = ['id', 'name'] 

class PestCropVarietyResponseSerializer(serializers.ModelSerializer):  
    crop = PestCropResponseSerializer()

    class Meta:
        model = CropVariety
        fields = ['id', 'name', 'local_name', 'crop'] 

class PestCropStageResponseSerializer(serializers.ModelSerializer):  
    variety = PestCropVarietyResponseSerializer()

    class Meta:
        model = CropStage
        fields = ['id', 'name', 'variety'] 

class PesteResponseSerializer(serializers.ModelSerializer): 
    infestation_stages = PestCropStageResponseSerializer()
    doses = serializers.SerializerMethodField('get_pest_doses')
    image = serializers.SerializerMethodField('get_image_url')

    class Meta:
        model = Pest
        fields = ['id', 'name', 'favourable_weather_condition', 'image', 'infestation_stages', 'doses']

    def get_pest_doses(self, Pest): 
        return PesteDoseConfiguration.objects.filter('id').values('id', 'generic_name', 'remedial_action', 'preventive_action')
    def get_image_url(self, Pest): 
        return MEDIA_URL+str(Pest.image)

** **

class PesteListView(generics.ListAPIView):
        Purpose: list of Pest details

        Method: GET
                stage_id: Positive Integer 

            JSON response containing message, status and data if applicable:
                    status: Positive Integer
                    next: URL
                    previous: URL
                    limit: Positive Integer
                    results: List of JSON
                    message: JSON
                    status: Positive Integer
    permission_classes = (IsAuthenticated,)
    queryset = Pest.objects.all()
    serializer_class = PesteResponseSerializer 

    def get_queryset(self):
        user = self.request.user 
        req_serializer = PesteCustomRequestSerializer(data=self.request.GET.dict())
        if req_serializer.is_valid():   
            return Pest.objects.filter(infestation_stages=self.request.GET['stage_id']).order_by('id') 
        raise CustomAPIException(req_serializer.errors) 

** **

from rest_framework.exceptions import APIException
from rest_framework import status 

class CustomAPIException(APIException):
    status_code = status.HTTP_400_BAD_REQUEST
Back to Top