Detail django's serializer serialization model several methods

  • 2020-12-18 01:51:32
  • OfStack

Serialization is the process of converting an object's state to a format that can be retained or transmitted. The opposite of serialization is deserialization, which converts a stream into an object. Together, these two processes make it easy to store and transfer data

Method 1: Override the get method by inheriting the View class to get the first 10 items of data before the commodity, serialize the data through serialize, and return the data response.


from django.views.generic import View
from goods.models import Goods
from django.core import serializers
from django.http import HttpResponse
 
class GoodsListView(View):
 
  def get(self,request):
   """
    through django the view Implement the product list page 
   :param request:
   :return:
   """
   # Returns the previous of all previous commodities 10 The data 
   goods_list = Goods.objects.all()[:10]
   # Direct conversion to json String of type 
   data = serializers.serialize("json",goods_list)
   # Note the addition of: "application/json" Otherwise the display in the browser will not be normal 
   return HttpResponse(data,"application/json")

Method 2: By inheriting the View class, override the get method to get the first 10 pieces of data specified to get certain field items, serialize the data through serialize, and return the data response.


from django.views.generic import View
from goods.models import Goods
from django.http import HttpResponse
import json
class GoodsListView(View):
  # rewrite get methods 
  #django Returns the first 1 Kind of way 
  def get(self,request):
    #  Before we get the goods 10 The data 
     goods=Goods.objects.all()[:10]
     goods_list=[]
     for good in goods:
       item={}
       item['name']=good.name
       item['click_num']=good.click_num
       item['shop_price']=good.shop_price
       goods_list.append(item)
    response_data=json.dumps(goods_list,ensure_ascii=False)
    #python string   Set to utf-8 coding 
    return HttpResponse(response_data,'application/json')

Method 3: Using Jsonresponse, we override get method by inheriting View class to get the first 10 items of data before goods, serialize data through serialize and return data response. In our work, we often use the third method to return JSON data.


from django.views.generic import View
from goods.models import Goods
from django.core import serializers
import json
from django.http import JsonResponse
class GoodsListView(View):
  def get(self,request):
   """
    through django the view Implement the product list page 
   :param request:
   :return:
   """
   # Returns the previous of all previous commodities 10 The data 
   goods_list = Goods.objects.all()[:10] 
   # Serialize, put in memory python Object, transformed into an object that can be transferred over the network 
   # Direct conversion to json String of type 
   data = serializers.serialize("json",goods_list)
   # Convert to dictionary 
   data = json.loads(data)
   return JsonResponse(data,safe=False)

Related articles: