def get(self, request, pk=None): ret = { 'cat1': '', 'cat2': '', 'cat3': '', } category = self.get_object() if category.parent is None: # 当前类别为一级类别 # 通过 频道 查询 类别: category.goodschannel_set.all()[0] ret['cat1'] = ChannelSerializer( category.goodschannel_set.all()[0]).data elif category.goodscategory_set.count() == 0: # 当前类别为三级 ret['cat3'] = CategorySerializer(category).data cat2 = category.parent ret['cat2'] = CategorySerializer(cat2).data ret['cat1'] = ChannelSerializer( cat2.parent.goodschannel_set.all()[0]).data else: # 当前类别为二级 ret['cat2'] = CategorySerializer(category).data ret['cat1'] = ChannelSerializer( category.parent.goodschannel_set.all()[0]).data return Response(ret)
def get(self, request): # 定义一个列表,存放返回数据 goods_cat_lst = [] # 获取一级目录对象列表 bcats = GoodsCategory.objects.filter(parent=0) # 获取每个一级目录 for bcat in bcats: bcat_data = CategorySerializer(bcat).data # 字典类型 # 查询该目录下的所有二级目录 scats = bcat.goodscategory_set.all() # set类型 # 存放该目录下所有二级目录的id ids = [] for scat in scats: ids.append(scat.id) # 获取该目录下所有二级目录的商品 goods_query_set = Goods.objects.filter( category_id__in=ids)[:5] # set goods = GoodsSerializer(goods_query_set, many=True).data # set bcat_data['goods'] = goods goods_cat_lst.append(bcat_data) return Response(goods_cat_lst)
def get(self, request): goods_dict = {} recommend_queryset = Goods.objects.filter(is_red=1).order_by("-sales") s = RecommendSerializer(recommend_queryset, many=True) goods_dict['recommend'] = s.data category1_queryset = GoodsCategory.objects.filter(parent=0) advertisement_list = [] for category1 in category1_queryset: category1_serializer = CategorySerializer(category1) category2_queryset = GoodsCategory.objects.filter(parent_id=category1.id) category2_id_list = [] for sub_category in category2_queryset: category2_id_list.append(sub_category.id) content_queryset = Goods.objects.filter(category_id__in=category2_id_list).order_by("-create_time") # print(len(content_queryset)) content_serializer = GoodsSerializer(content_queryset, many=True) category_data = category1_serializer.data category_data["goods"] = content_serializer.data advertisement_list.append(category_data) goods_dict['advertisement'] = advertisement_list return Response(goods_dict)
class GoodsSerializer(serializers.ModelSerializer): #覆盖外键字段 category = CategorySerializer() #images是数据库中设置的related_name="images",把轮播图嵌套进来 images = GoodsImageSerializer(many=True) class Meta: model = Goods fields = '__all__'
def get(self, request, pk=None): """获取面包屑导航""" # 返回数据字典 ret = { # 一级类别 'cat1': '', # 二级类别 'cat2': '', # 三级类别 'cat3': '', } # 当前商品类别 category = self.get_object() # 如果商品类别没有父类 -- [一级类别] if category.parent is None: # 通过 频道 查询 类别: category.goodschannel_set.all()[0] # 设置[一级类别]数据 ret['cat1'] = ChannelSerializer( category.goodschannel_set.all()[0]).data # 如果当前商品类别没有子类别 -- [三级类别] elif category.goodscategory_set.count() == 0: # 设置[三级类别]数据 ret['cat3'] = CategorySerializer(category).data # 获取[二级类别]数据 cat2 = category.parent # 设置[二级类别]数据 ret['cat2'] = CategorySerializer(cat2).data # 设置[一级类别]数据 ret['cat1'] = ChannelSerializer( cat2.parent.goodschannel_set.all()[0]).data # 否则 -- [二级类别] else: # 设置[二级类别]数据 ret['cat2'] = CategorySerializer(category).data # 设置[一级类别]数据 ret['cat1'] = ChannelSerializer( category.parent.goodschannel_set.all()[0]).data # 返回封装好的数据 return Response(ret)
def get(self, request): top_dict = {} # 轮播图商品 slide_queryset = Goods.objects.filter(is_slide=1, status=0).all() ser = GoodsSerializer(instance=slide_queryset, many=True) top_dict['slide_goods'] = ser.data # 推荐商品 recommend_queryset = Goods.objects.filter(is_red=1, status=0).all()[0:4] ser = GoodsSerializer(instance=recommend_queryset, many=True) top_dict['recommend_goods'] = ser.data # 商品分类 category_queryset = GoodsCategory.objects.filter(parent_id=0).all() ser = CategorySerializer(instance=category_queryset, many=True) top_dict['category_goods'] = ser.data return Response(top_dict)
def get(self, request): # 一级分类 category_queryset = GoodsCategory.objects.filter(parent_id=0).all() print(len(category_queryset)) data_list = [] for category in category_queryset: # 一级分类 data_dict = CategorySerializer(instance=category).data # 获得属于该一级分类下的所有二级分类的id sub_list = category.goodscategory_set.all() id_list = [] for sub in sub_list: id_list.append(sub.id) # 属于该一级分类的商品 good_queryset = Goods.objects.filter(category_id__in=id_list, status=0).all() data_dict['goods'] = GoodsSerializer(instance=good_queryset, many=True).data data_list.append(data_dict) return Response(data_list)