Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

django serializer

# serialization in django

from rest_framework import serializers
from .models import Product

class ProductSerializers(serializers.ModelSerializer):
    class Meta:
        model = Product
        fields = '__all__'
        
# adding extra fields, that are in models
    def to_representation(self, instance):
      data = super().to_representation(instance)
      data['is_on_sale'] = instance.is_on_sale()
      data['current_price'] = instance.current_price()
      return data
Comment

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 :: ValueError: query data dimension must match training data dimension 
Python :: range function 
Python :: python swap numbers 
Python :: python list to dict 
Python :: hash python png 
Python :: python loop shorthand 
Python :: download folder collab 
Python :: anaconda 
Python :: rust vs python 
Python :: postgresql backup using python 
Python :: reorder list python 
Python :: how to count specific element in a list python 
Python :: run a python script from another python script on a raspberry pi 
Python :: List Delete a Element 
Python :: create a python api 
Python :: savefig matplotlib python 
Python :: send serial commands in python 
Python :: read pickle file 
Python :: random seed generator minecraft 
Python :: use mongo replica set python 
Python :: dataframe check for nan in iterrows 
Python :: python rotate list 
Python :: python code for extracting data from pdf 
Python :: pandas dataframe map 
Python :: how to redirect user in flask response python 
Python :: register admin django 
Python :: flask structure 
Python :: add values to tuple python 
Python :: iterate over a set python 
Python :: new line print python 
ADD CONTENT
Topic
Content
Source link
Name
2+6 =