轮模图和商品页面

先定义一个模型类

from django.db import models

# Create your models here.
class Category(models.Model):
    cate_name = models.CharField(max_length=50,verbose_name='类别名')
    class Meta:
        verbose_name_plural = '商品类别'
        db_table = 'category'
    def __str__(self):
        return self.cate_name

再定义序列化器

from rest_framework import serializers
from .models import Goods,Category,GoodImg,Carousel
class catser(serializers.ModelSerializer):
    class Meta:
        model = Category
        fields = '__all__'

class goodser(serializers.ModelSerializer):
    class Meta:
        model = Goods
        fields = '__all__'

class goodimgser(serializers.ModelSerializer):
    class Meta:
        model = GoodImg
        fields = '__all__'

class carser(serializers.ModelSerializer):
    class Meta:
        model = Carousel
        fields = '__all__'

在view页面写轮模图

class GetCarousel(APIView):
    def get(self,requst):
        # 查询结果集
        car_data = Carousel.objects.all()

        # 序列化  ORM->JSON数据
        ser = carser(car_data,many=True)
        return Response({
            'code':200,
            'msg':'加载成功',
            'carousel':ser.data
        })

获取某一类商品

class OnceCat(APIView):
    def get(self,request):
        # 获取种类信息
        cate_name = request.query_params.get('categoryName')
        # ORM操作获取种类信息
        try:
            cate_data = Category.objects.get(cate_name=cate_name)
        except Exception as e:
            print(e)
            return Response({'msg':'此类不存在','code':204})
        # 根据类别  获取全部商品
        goods_data = cate_data.goods_set.all()
        # 序列化
        ser = goodser(goods_data,many=True)
        return Response({
            'msg':'成功获取',
            'code':200,
            'result':ser.data
        })

获取热门商品

class HotProduct(APIView):
    def get(self, request):
        # 获取热门商品是一个列表
        cate_name = request.query_params.get('categoryName')
        # ORM操作获取种类信息
        cate_list = request.query_params.getlist('categoryName[]')
        # 根据类别信息获取种类信息(ORM数据)
        cate_data_list=[]
        for i in cate_list:
            cate_data = Category.objects.get(cate_name=i)
            cate_data_list.append(cate_data)
        # 根据类别信息获取商品信息
        good_data_list = []
        for i in cate_data_list:
            good_data_list.append(i.goods_set.all())
        # 将商品信息合并
        from functools import reduce
        goods_data_queryset = reduce((lambda x,y:x|y),good_data_list).order_by('count')

        # 序列化
        ser = goodser(goods_data_queryset,many=True)
        return Response({
            'msg':'成功获取',
            'code':200,
            'result':ser.data
        })

获取一类商品信息

class onegood(APIView):
    def get(self,request):
#         获取id
        good_id = request.query_params.get('productID')
        try:
            good = Goods.objects.get(id=good_id)
        except Exception as e:
            print(e)
            return Response({'msg':'商品不存在','code':404})
        # 序列化数据
        ser = goodser(good)
        return Response(ser.data)

获取一类商品信息的图片

class oneimg1(APIView):
    def get(self,request):
        # 获取图片的id
        imgid = request.query_params.get('productID')
        try:
            # 判断是否存在id
            good_data = Goods.objects.get(id=imgid)
        except Exception as a:
            print(a)
            return Response({'msg':'图片不存在','code':404})
        # good_imgs = good.goodimg_set.all()
        # 有主键获取子表的图片
        # ?????
        aa = good_data.goodimg_set.all()
        # 序列化
        ser = goodimgser(aa,many=True)
        return Response(ser.data)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值