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
Custom validations for serializer fields Django Rest Framework

we can write custom validations for serializer fields in Django Rest Framework. Validators are used to validate the data whether it is semantically valid or ...

Continue Reading...
Using Python weasyprint generate HTML to PDF in django

WeasyPrint is to combine multiple pieces of information into an HTML template and then converting it to a PDF document.

Continue Reading...
Working with Django Plugins

This blog describes about how to work with django-plugins. Django Plugin is a Simple Plugin Framework for Django. By using django-plugins, you can make your ...

Continue Reading...

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