zoukankan      html  css  js  c++  java
  • 微信:商品分类 商品列表,详情,热销

    一 商品分类

     

    1.接口Category.py
    
    from rest_framework.views import APIView
    from rest_framework.response import  Response
    from  app01 import models
    from app01.my_ser import Category_ser
    from app01.comment import func
    class All(APIView):
        def post(self,request):
            data=models.Category.objects.filter(is_show=True)
            data=Category_ser.Category_ser(instance=data,many=True,context={"request":request}).data
            data=func.get_son(data)
            return Response({
                "code":200,
                "msg":"ok",
                "data":data
            })
    
    2.序列化Category_ser
    from  rest_framework import serializers
    from app01 import  models
    class Category_ser(serializers.ModelSerializer):
        image_url=serializers.ImageField(source="image.image_url")
        parent_id=serializers.SerializerMethodField()
        def get_parent_id(self,obj):
            if obj.parent_id is None:
                return 0
            else:
                return  obj.parent_id
    
        class Meta:
            model=models.Category
            fields="__all__"
    
    3.func.py
    
    res=[]
    def  get_son(data,level=0,parent_id=0,is_clear=True):
        if is_clear:
            res.clear()
        for item in data:
            if item['parent_id']==parent_id:
                item['level']=level
                res.append(item)
                get_son(data,level=level+1,parent_id=item['cat_id'],is_clear=False)
        return res
    """
    son=get_son(data)
    print(son)
    [{'cat_id': 1, 'name': '北京', 'parent_id': 0, 'level': 0}, {'cat_id': 3, 'name': '沙河', 'parent_id': 1, 'level': 1}, {'cat_id': 4, 'name': 'sb镇', 'parent_id': 3, 'level': 2}, {'cat_id': 5, 'name': '昌平', 'parent_id': 1, 'level': 1}, {'cat_id': 2, 'name': '上海', 'parent_id': 0, 'level': 0}, {'cat_id': 6, 'name': '青浦', 'parent_id': 2, 'level': 1}]
    """
    商品分类

    二 商品列表,详情,热销

    from rest_framework.views import APIView
    from rest_framework.response import  Response
    from app01.my_ser import Goods_ser
    from  app01 import models
    from app01.my_ser import Category_ser,Goods_ser
    from app01.comment import func
    #热销商品
    class HotGoods(APIView):
        def post(self,request):
            data=models.Product.objects.filter(disabled=True).order_by("-buy_count","-w_order")
            data=Goods_ser.Goods_ser(instance=data,many=True,context={"request":request}).data
            return  Response({"code":200,"msg":"ok","data":data})
    #商品列表
    class List(APIView):
        def post(self,request):
            param=request.data
            #category_id为某个分类的id
            if param.get('category_id'):
                data=models.Category.objects.filter(is_show=True)
                data=Category_ser.Category_ser(instance=data,many=True,context={"request":request}).data
                #从date列表中找出所有category_id的子分类孙分类包括自身
                all_id=func.get_son_id(data,param['category_id'])
                #把自身和孙分类查出序列化
                data=models.Product.objects.filter(disabled=True,cat_id__in=all_id).order_by("-w_order")
                data = Goods_ser.Goods_ser(instance=data, many=True, context={"request": request}).data
                return Response({"code": 200, "msg": "ok", "data": data})
            else:
                return Response({"code": 201, "msg":"缺少参数" })
    
    #商品详情,前端传回摸个具体商品的主键
    class Detail(APIView):
        def post(self,request):
            param=request.data
            if param.get("id"):
                data = models.Product.objects.filter(disabled=True,product_id=param.get("id")).first()
                if data:
                    data = Goods_ser.Goods_ser(instance=data, many=False, context={"request": request}).data
                    print(data)
                    return Response({"code": 200, "msg": "ok", "data": data})
                else:
                    return Response({"code": 201, "msg": "没有该商品"})
    商品

    序列化

    from  rest_framework import serializers
    from app01 import  models
    class Goods_ser(serializers.ModelSerializer):
        image_url=serializers.ImageField(source="image.image_url")
        stock=serializers.IntegerField(source="stock.quantity")
        class Meta:
            model=models.Product
            fields="__all__"
    序列化

  • 相关阅读:
    Numpy数据处理函数
    数据库-创建表
    描述统计学:表格法和图形法
    最短路 || POJ 1511 Invitation Cards
    搜索 || DFS || UOJ 146 信息传递
    最短路 || UOJ 19 寻找道路
    最短路 || POJ 1847 Tram
    博弈论
    筛选法 || POJ 3292 Semi-prime H-numbers
    团队第一次作业
  • 原文地址:https://www.cnblogs.com/tfzz/p/12595721.html
Copyright © 2011-2022 走看看