码迷,mamicode.com
首页 > 微信 > 详细

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

时间:2019-05-14 09:40:10      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:you   row   类别   django   shortcut   EAP   time   response   code   

1.获取类别逻辑:

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

from rest_framework import serializers
from .models import Category,Good,Prize,Images


class 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 render
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.renderers import JSONRenderer,BrowsableAPIRenderer
from .serializers import CategoryModelSerializer,GoodModelSerializer,PrizeModelSerializer,ImagesModelSerializer
from .models import Category,Good,Images,Prize
from 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 path
from .views import GetCategory

urlpatterns = [
    path(getcategory/,GetCategory.as_view())
]

 

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

标签:you   row   类别   django   shortcut   EAP   time   response   code   

原文地址:https://www.cnblogs.com/xuepangzi/p/10860121.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!