Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

serialization in django

serialization in django

from rest_framework import serializers
from .models import Product

class ProductSerializers(serializers.ModelSerializer):
    class Meta:
        model = Product
        fields = '__all__'
        
# Serializers allow complex data such as querysets and model instances to be converted to native Python datatypes that can then be easily rendered into JSON, XML or other content types. 
# Serializers also provide deserialization, allowing parsed data to be converted back into complex types, after first validating the incoming data.
Comment

what is serialization in django

It is just a method of converting a type of data into another, most probably in 
text format, and in dictionary or into a human managable form
Comment

what is serializer in django

Serializers in Django REST Framework are responsible for converting objects into data types understandable by javascript and front-end frameworks.
Serializers also provide deserialization, allowing parsed data to be converted back into complex types,
after first validating the incoming data.
Comment

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' )

        
Comment

PREVIOUS NEXT
Code Example
Python :: create jwt token in django 
Python :: get schema of json pyspark 
Python :: demonstrating polymorphism in python class 
Python :: remove a first array of item in python 
Python :: pip config proxy 
Python :: how to convert categorical data to numerical data in python 
Python :: tkinter canvas text 
Python :: TfidfVectorizer use 
Python :: pandas shape 
Python :: python *args and **kwargs 
Python :: disable sns plot python 
Python :: python calculations with variable x (letter) 
Python :: tkinter how to update optionmenu contents 
Python :: sys module in python 
Python :: numpy iterate over rows with index 
Python :: opkg install python-lxml_2.2.8-r1_mips32el.ipk 
Python :: python requests with authorisation token 
Python :: sklearn grid search show progress 
Python :: combine column in csv python pandas 
Python :: for loop in python array 
Python :: import in python 
Python :: alexa python get slot value 
Python :: pd.loc 
Python :: how to calculate numbers with two zeros in python 
Python :: increment dic with for loop 
Python :: rsa decryption 
Python :: tar dataset 
Python :: how does a neural network work 
Python :: python text recognition 
Python :: viewset and router 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =