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

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 :: upper python 
Python :: what does << do in python 
Python :: python range for loop 
Python :: how to open any application in python 
Python :: python file browser 
Python :: default arguments 
Python :: requests encoding python 
Python :: unban member using ID discord.py 
Python :: pandas groupby and keep columns 
Python :: syntax error python 
Python :: how to make dice roll in python 
Python :: TypeError: can only concatenate str (not "list") to str 
Python :: python bytes to hex 
Python :: whatsapp bot python code 
Python :: global variable in python 
Python :: remove dict python 
Python :: convert spark dataframe to pandas 
Python :: Python How To Convert a String to Variable Name 
Python :: python loop dictionary 
Python :: add new column to pandas dataframe 
Python :: car python program 
Python :: how to read a excel file in python 
Python :: how to debug python code in visual studio code 
Python :: mapping in python 
Python :: tri python 
Python :: re python 
Python :: tkinter filedialog 
Python :: how to make a calculator 
Python :: add to list python 
Python :: flatten dict with lists as entries 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =