DRF框架基础四之二次封装Response,数据库关系分析,ORM操作关系,序列化和十大接口
Posted jiangxianseng
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DRF框架基础四之二次封装Response,数据库关系分析,ORM操作关系,序列化和十大接口相关的知识,希望对你有一定的参考价值。
一.二次封装Response
之前我们在视图类中返回响应结果是下面这种形式
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from . import models, serializers
class CarAPIView(APIView):
def get(self, request, *args, **kwargs):
pk = kwargs.get(‘pk‘)
if pk:
try:
car_obj = models.Car.objects.get(pk=pk)
car_serializer = serializers.CarModelSerializer(car_obj, many=False)
return Response({
‘status‘: 0,
‘msg‘: ‘ok‘,
‘result‘: car_serializer.data
})