广告小程序后端开发(6.获取类别逻辑)

1.获取类别逻辑:

1.在apps/ad目录下新建serializers.py文件进行序列化:

from rest_framework import serializersfrom .models import Category,Good,Prize,Imagesclass CategoryModelSerializer(serializers.ModelSerializer): class Meta: model = Category fields="__all__"class GoodModelSerializer(serializers.ModelSerializer): class Meta: model = Good fields="__all__"class PrizeModelSerializer(serializers.ModelSerializer): class Meta: model = Prize fields="__all__"class ImagesModelSerializer(serializers.ModelSerializer): class Meta: model = Images fields="__all__"

2.在apps/ad/views.py中编写获取类别数据逻辑:

from django.shortcuts import renderfrom rest_framework.views import APIViewfrom rest_framework.response import Responsefrom rest_framework.renderers import JSONRenderer,BrowsableAPIRendererfrom .serializers import CategoryModelSerializer,GoodModelSerializer,PrizeModelSerializer,ImagesModelSerializerfrom .models import Category,Good,Images,Prizefrom datetime import datetime# Create your views here.class GetCategory(APIView): """ 获取类别数据 """ renderer_classes = [JSONRenderer] # 渲染器 def get(self, request): category_list = Category.objects.all() re = CategoryModelSerializer(category_list, many=True) return Response(re.data)

3.在apps/ad目录下新建urls.py:

from django.urls import pathfrom .views import GetCategoryurlpatterns = [ path(getcategory/,GetCategory.as_view())]

 

相关文章