Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

serialization in python

serialization in django

from rest_framework import serializers
from .models import Student

class ProductSerializers(serializers.ModelSerializer):
    class Meta:
        model = Student
        fields = '__all__'
#in views.py
from django.shortcuts import render
from .serializers import StudentSerializer
from django.http import HttpResponse
from .models import Student
from rest_framework.renderers import JSONRenderer


# Create your views here.
def students(request):
    stu = Student.objects.all()
#    stu = Student.objects.get(id=2)  to get one object
#    serializer = StudentSerializer(stu)  we dont need many=True
    serializer = StudentSerializer(stu, many=True)
    json_data = JSONRenderer().render(serializer.data)
    print(serializer)
    return HttpResponse(json_data, content_type ='application/json' )

        
 
PREVIOUS NEXT
Tagged: #serialization #python
ADD COMMENT
Topic
Name
5+5 =