Customizing Django REST API Serializers

Adding extra fields to Serializers

# serializers.py
from rest_framework import serializers
from test_app.models import Product

class ProductSerializer(serializers.Serializer):
    product_id = serializers.IntegerField()
    product = serializers.SerializerMethodField('get_product_name')

    def get_product_name(self, obj):
        if obj.get("product_id"):
            obj_product = Product.objects.filter(id=obj.get("product_id")).first()
            if obj_product:
                return obj_product.name
        return None

# API views.py
from rest_framework.decorators  import api_view
from rest_framework.response import Response
from rest_framework import status
from test_app.serializers import ProductsSerializer

@api_view(['GET'])
def list_products(request):
    response_data = {}
    response_data["data"] = ProductSerializer(
                                    [{"program": i} for i in [1, 2, 3]],
                                   many=True
                                ).data
    return Response(response_data, status=status.HTTP_200_OK)

Passing extra arguments to Serializer Class in Django Rest Framework

Suppose we have searlizer called 'CollegeStudentsSerializer' which should return all students, branches details of a logged-in user college

# serializers.py
from rest_framework import serializers
from test_app.models import Student, Branch

class StudentSerializer(serializers.ModelSerializer):

    class Meta:
        model = Student
        fields = ['id', 'first_name', 'last_name', 'branch']

class BranchSerializer(serializers.ModelSerializer):

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

class CollegeDetailsSerializer(serializers.Serializer):
    students = serializers.SerializerMethodField('get_students')
    branches = serializers.SerializerMethodField('get_branches')

    def __init__(self, *args, **kwargs):
        context = kwargs.pop("context")
        self.college_id = context.get('college_id')
        super(CollegeDetailsSerializer, self).__init__(*args, **kwargs)

    def get_students(self, obj):
        return StudentSerializer(
                    Student.objects.filter(college_id=self.college_id),
                    many=True
                ).data

    def get_branches(self, obj):
        return BranchSerializer(
                    Branch.objects.filter(college_id=self.college_id),
                    many=True
                ).data

# API views.py
from rest_framework.decorators  import api_view
from rest_framework.response import Response
from rest_framework import status
from test_app.serializers import CollegeDetailsSerializer

@api_view(['GET'])
def students_list(request):
    response_data = {"data": []}
    if request.user and request.user.college_id:
        response_data["data"] = CollegeDetailsSerializer(
                                    context={"college_id": request.user.college_id},
                                    many=True
                                ).data
    return Response(response_data, status=status.HTTP_200_OK)

Posted On 09 January 2019 By MicroPyramid


Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
Django Forms basics explained

Django forms is powerful module to help django application development in rendering html from model, validate input from http request to model specifications. And we ...

Continue Reading...
Handling Custom Error Pages(404, 500) In Django

404 Page not found and 500 Internal server errors generally occur in every website. When these errors occurs, generally for Django application it will load ...

Continue Reading...
Dynamically Adding Google Maps with Marker In Django

Google Maps allows you to display maps on your web site, we can also customize maps, and the information on maps.
The Google Maps API ...

Continue Reading...
open source packages

Subscribe To our news letter

Subscribe and Stay Updated about our Webinars, news and articles on Django, Python, Machine Learning, Amazon Web Services, DevOps, Salesforce, ReactJS, AngularJS, React Native.
* We don't provide your email contact details to any third parties