def get(self, request, pk): ordering = request.query_params.get("ordering") ordering_list = ordering.split(',') # print(ordering_list) # print(type(ordering_list)) category_obj = GoodsCategory.objects.get(id=pk) goods_dict = {} if not category_obj.parent_id: goods_dict["category_guide"] = ParentCategorySerializer(category_obj).data category2_id_list = [] # sub_category_queryset = category_obj.category_set.all() sub_category_queryset = GoodsCategory.objects.filter(parent_id=category_obj.id) for sub_category in sub_category_queryset: category2_id_list.append(sub_category.id) content_queryset = Goods.objects.filter(category_id__in=category2_id_list).order_by(*ordering_list) goods_dict["goods_content"] = GoodsSerializer(content_queryset, many=True).data else: goods_dict["category_guide"] = ParentCategorySerializer(category_obj).data sub_content_queryset = Goods.objects.filter(category_id=category_obj.id).order_by(*ordering_list) goods_dict["goods_content"] = GoodsSerializer(sub_content_queryset, many=True).data return Response(goods_dict)
def put(self, request, pk, format=None): snippet = self.get_object(pk) serializer = GoodsSerializer(snippet, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
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)
class ShopCartDetatilSerializer(serializers.ModelSerializer): goods = GoodsSerializer( many=False) #内嵌goods的Serializer,many=False表示一个ShopCart只能对应一个goods class Meta: model = ShoppingCart fields = "__all__" #返回全部字段
class ShopCartDetailSerializer(serializers.ModelSerializer): goods = GoodsSerializer(many=False, read_only=True) class Meta: model = ShoppingCart # fields = ("goods", "nums") fields = "__all__"
class OrderGoodsSerializer(serializers.ModelSerializer): """订单中的商品""" goods = GoodsSerializer(many=False) class Meta: model = OrderGoods fields = "__all__"
class UserFavDetailSerializer(serializers.ModelSerializer): # 通过goods_id拿到商品信息。就需要嵌套的Serializer goods = GoodsSerializer() class Meta: model = UserFav fields = ("goods", "id")
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)
class OrderGoodsSerialzier(serializers.ModelSerializer): goods = GoodsSerializer(many=False) # 支付订单的url alipay_url = serializers.SerializerMethodField(read_only=True) def get_alipay_url(self, obj): alipay = AliPay( appid="2016091500517456", app_notify_url="http://47.93.198.159:8000/alipay/return/", app_private_key_path=private_key_path, alipay_public_key_path= ali_pub_key_path, # 支付宝的公钥,验证支付宝回传消息使用,不是你自己的公钥, debug=True, # 默认False, return_url="http://47.93.198.159:8000/alipay/return/") url = alipay.direct_pay( subject=obj.order_sn, out_trade_no=obj.order_sn, total_amount=obj.order_mount, ) re_url = "https://openapi.alipaydev.com/gateway.do?{data}".format( data=url) return re_url class Meta: model = OrderGoods fields = "__all__"
class OrderGoodsSerializer(serializers.ModelSerializer): # 外键关系 所以使用False goods = GoodsSerializer(many=False) class Meta: model = OrderGoods fields = '__all__'
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 OrderGoodsSerializer(serializers.ModelSerializer): # odergood 的 外键 goods,非多的那一头 goods = GoodsSerializer() class Meta: model = OrderGoods fields = '__all__'
class UserShoppingCartDeatilSerializer(serializers.ModelSerializer): # 外键 ,一个购物车商品记录只能对应一件商品 goods = GoodsSerializer(read_only=True) class Meta: model = ShoppingCart fields = ("goods", "nums")
class CartSerializer(serializers.ModelSerializer): # 方法3 goods = GoodsSerializer() class Meta: model = CartModel fields = '__all__' def to_representation(self, instance): data = super().to_representation(instance) data['c_is_select'] = data['is_select'] data['c_goods_num'] = data['c_num'] # 序列化商品对象信息 方法1 # goods = Goods.objects.filter(id=data['goods']).first() # goods = instance.goods # data['c_goods'] = GoodsSerializer(goods).data # 方法2 # data['c_goods'] = { # 'id': instance.goods.id, # 'price': instance.goods.price # } data['c_goods'] = data['goods'] del data['c_num'] del data['user'] del data['is_select'] del data['goods'] return data
class OrderGoodsSerializer(serializers.ModelSerializer): #订单商品和商品简历一对一的关系 goods = GoodsSerializer(many=False) class Meta: model = OrderGoods fields = "__all__"
def get(self, request): """ 首页: 获取商品首页右侧的推荐商品 """ goods = Goods.objects.filter(is_red=1).order_by('-create_time')[0:4] serializer = GoodsSerializer(goods, many=True) return Response(serializer.data)
def to_representation(self, instance): # 这儿重构是购物车里商品是否被选中(is_select), # 商品数量(c_num),商品本身的信息(goods) data = super().to_representation(instance) data['c_is_select'] = data['is_select'] data['c_goods_num'] = data['c_num'] data['c_goods'] = data['goods'] # 序列化商品对象信息,方法1 # goods = Goods.objects.filter(id=data['goods']).first() # instance是代表购物车 ,instance.goods代表从购物车里拿出数据 goods = instance.goods # 将购物车里的商品数据进行序列化 # 这里序列化是显示商品的名称,价格,图片 data['c_goods'] = GoodsSerializer(goods).data # 序列化商品对象信息,方法2 # data['c_goods'] = { # 'id': instance.goods.id, # 'price': instance.goods.price, # } del data['c_num'] del data['user'] del data['is_select'] del data['goods'] return data
class UserFavDetailSerializer(serializers.ModelSerializer): goods = GoodsSerializer() class Meta: model = UserFav fields = ('id', 'goods')
class OrderGoodsSerializer(serializers.ModelSerializer): # order 是model的 order的related_name 的名字 order = GoodsSerializer(many=False) class Meta: model = OrderGoods fields = "__all__"
class OrderGoodsSerializer(serializers.ModelSerializer): # 订单商品和商品的关系--一对一关系,序列化只需要一条 goods = GoodsSerializer(many=False) class Meta: model = OrderGoods fields = "__all__"
class UserFavDetailSerializer(serializers.ModelSerializer): # 通过good_id拿到商品信息,需要嵌套使用 goods = GoodsSerializer() class Meta: model = UserFav fields = ('goods', 'id')
class ShopCartDetailSerializer(serializers.ModelSerializer): # 一条购物车关系记录对应的只有一个goods。 goods = GoodsSerializer(many=False, read_only=True) class Meta: model = ShoppingCart fields = ("goods", "nums")
class CartSerializer(serializers.ModelSerializer): c_goods = GoodsSerializer() class Meta: model = Cart fields = '__all__'
class UserFavDetailSerializer(serializers.ModelSerializer): goods = GoodsSerializer() class Meta: model = UserFav fields = ("goods", "id")
class OrderGoodsSerializer(serializers.ModelSerializer): o_goods = GoodsSerializer() class Meta: model = OrderGoods fields = '__all__'
class OrderGoodsSerializer(serializers.ModelSerializer): # 这个字段可不唯一,所以many=False goods = GoodsSerializer(many=False) class Meta: model = OrderGoods fields = "__all__"
class UserFavDetailSerializer(serializers.ModelSerializer): # goods = GoodsSerializer(many=True) #设置goods嵌套序列 goods = GoodsSerializer() # 设置goods嵌套序列,goods是个外键,不是对应多个,而是对应一个实例 class Meta: model = UserFav #model为UserFav fields = ("goods", "id") #设置fields
class ShopCartDetailSerializer(serializers.ModelSerializer): # 设置动态serializer goods = GoodsSerializer(many=False) class Meta: model = ShoppingCart fields = '__all__'
def retrieve(self, request, pk=None): queryset = GoodsInStore.objects.filter(store_id=pk) goods = [] for itm in queryset: goods.append(itm.goods) serializer = GoodsSerializer(goods, many=True) return Response(serializer.data)
class UserFavDetailSerializer(serializers.ModelSerializer): #fav对应商品 他自己就是外键 对应的就是一个对象 goods = GoodsSerializer() class Meta: model = UserFav fields = ("goods", "id")