Vue+Django2.0 restframework打造前后端分离的生鲜电商项目(3)

1.drf前期准备

1.django-rest-framework官方文档

https://www.django-rest-framework.org/
#直接百度找到的djangorestframework的官网是打不开的

2.安装依赖包

Vue+Django2.0 restframework打造前后端分离的生鲜电商项目(3)_第1张图片

如图所示,django restframework的依赖模块,除了coreapi和django-guardian,已经在前面安装过了。

打开终端,执行安装命令

pip install django-guardian
pip install coreapi

3.将rest_framework在settings中注册

Vue+Django2.0 restframework打造前后端分离的生鲜电商项目(3)_第2张图片

4.管理调试api的时候会用到的url配置,在urls.py中

from django.urls import path

from django.views.static import serve
from MxShop.settings import MEDIA_ROOT
from django.urls import include

import xadmin

from rest_framework.documentation import include_docs_urls

from goods.views import GoodsListView

urlpatterns = [
    path('xadmin/', xadmin.site.urls),
    path('media/',serve,{'document_root':MEDIA_ROOT}),
    path('ueditor/',include('DjangoUeditor.urls' )),
    path('docs/',include_docs_urls(title="慕学生鲜")),
    path('api-auth/', include('rest_framework.urls')),
    path('goods/',GoodsListView.as_view(),name='goods-list'),
]

2.序列化商品数据

在goods目录下新建serializers.py文件

from rest_framework import serializers
from goods.models import Goods,GoodsCategory


class GoodsSerializer(serializers.Serializer):#Serializer方式序列化
    name=serializers.CharField(required=True,max_length=100)
    click_num=serializers.IntegerField(default=0)
    goods_front_image=serializers.ImageField()

    # 用于post
    def create(self, validated_data):
        return Goods.objects.create(**validated_data)


class CategoryModelSerializer(serializers.ModelSerializer):
    class Meta:
        model=GoodsCategory
        fields="__all__"#将整个表的所有字段都序列化


class GoodsModelSerializer(serializers.ModelSerializer):#ModelSerializer方式序列化
    category=CategoryModelSerializer()#外键信息嵌入
    class Meta:
        model=Goods
        # fields="__all__"#将整个表的所有字段都序列化
        fields=('name','goods_front_image','category')#指定序列化某些字段

 

 3.视图封装

1.使用APIView+Response实现商品列表页视图(最想原生django的CBV写法)

views.py中

from .serializers import GoodsSerializer,GoodsModelSerializer
from rest_framework.views import APIView
from rest_framework.response import Response

from .models import Goods
from rest_framework import status


class GoodsListView1(APIView):
    """
    List all goods,使用Serializer
    """
    def get(self, request, format=None):
        goods=Goods.objects.all()[:10]
        goods_serializer = GoodsSerializer(goods, many=True)
        return Response(goods_serializer.data)

    def post(self,request,format=None):
        serializer=GoodsSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data,status=status.HTTP_201_CREATED)
        return Response(serializer.errors,status=status.HTTP_400_BAD_REQUEST)


class GoodsListView2(APIView):
    """
    List all goods,使用ModelSerializer
    """
    def get(self, request, format=None):
        goods=Goods.objects.all()[:10]
        goods_serializer = GoodsModelSerializer(goods, many=True)
        return Response(goods_serializer.data)

urls.py中

from django.contrib import admin
from django.urls import path

from django.views.static import serve
from MxShop.settings import MEDIA_ROOT

import xadmin
from goods.views import GoodsListView1,GoodsListView2

urlpatterns = [
    path('admin/', admin.site.urls),
    path('xadmin/', xadmin.site.urls),
    path('media/',serve,{'document_root':MEDIA_ROOT}),
    path('ueditor/',include('DjangoUeditor.urls' )),

    path('goods/',GoodsListView1.as_view(),name='goods-list'),
    path('goods2/',GoodsListView2.as_view(),name='goods-list2')
]

 

2.使用mixins+generic实现商品列表页视图

from .serializers import GoodsSerializer
from .models import Goods
from rest_framework import mixins
from rest_framework import generics

class GoodsListView(mixins.ListModelMixin,generics.GenericAPIView):
    """
    商品列表页
    """
  queryset = Goods.objects.get_queryset().order_by('id')[:10]
   serializer_class = GoodsSerializer 

  def get(self,request,*args,**kwargs):

    return self.list(request,*args,**kwargs)

3.使用generics.ListAPIView实现商品列表页视图

from .serializers import GoodsSerializer
from .models import Goods
from rest_framework import generics

class GoodsListView(generics.ListAPIView):
    """
    商品列表页
    """
    queryset = Goods.objects.get_queryset().order_by('id')
    serializer_class = GoodsSerializer

 4.分页

1.全局分页:在settings中加入代码,所有的列表页,都会变成每10个一页的分页方式,显然这有很大的局限性

REST_FRAMEWORK={
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
    'PAGE_SIZE': 10,
}

 2.局部定制化分页:在view中定义代码

from rest_framework.pagination import PageNumberPagination

class GoodsPagination(PageNumberPagination):
    page_size = 10
    page_size_query_param = 'page_size'
    page_query_param = 'p'
    max_page_size = 100


class GoodsListView(generics.ListAPIView):
    """
    商品列表页
    """
    queryset = Goods.objects.get_queryset().order_by('id')
    serializer_class = GoodsSerializer
    pagination_class = GoodsPagination

 5.viewsets

1.在views中

from rest_framework import viewsets

class GoodsListViewSet(mixins.ListModelMixin,viewsets.GenericViewSet):
    """
    商品列表页
    """
    queryset = Goods.objects.get_queryset().order_by('id')
    serializer_class = GoodsSerializer
    pagination_class = GoodsPagination

2.在urls中

from goods.views import GoodsListViewSet

from rest_framework.routers import DefaultRouter
router = DefaultRouter()
router.register(r'goods', GoodsListViewSet,base_name="goods") 
urlpatterns
= [ path('', include(router.urls)) ]

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 
  

转载于:https://www.cnblogs.com/xuepangzi/p/9698874.html

你可能感兴趣的:(Vue+Django2.0 restframework打造前后端分离的生鲜电商项目(3))