예제 #1
0
파일: views.py 프로젝트: zdy456/cms
    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)
예제 #2
0
 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)
예제 #3
0
파일: views.py 프로젝트: Wise-zhang/cms
 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)
예제 #4
0
class ShopCartDetatilSerializer(serializers.ModelSerializer):
    goods = GoodsSerializer(
        many=False)  #内嵌goods的Serializer,many=False表示一个ShopCart只能对应一个goods

    class Meta:
        model = ShoppingCart
        fields = "__all__"  #返回全部字段
예제 #5
0
class ShopCartDetailSerializer(serializers.ModelSerializer):
    goods = GoodsSerializer(many=False, read_only=True)

    class Meta:
        model = ShoppingCart
        # fields = ("goods", "nums")
        fields = "__all__"
예제 #6
0
class OrderGoodsSerializer(serializers.ModelSerializer):
    """订单中的商品"""
    goods = GoodsSerializer(many=False)

    class Meta:
        model = OrderGoods
        fields = "__all__"
예제 #7
0
class UserFavDetailSerializer(serializers.ModelSerializer):
    # 通过goods_id拿到商品信息。就需要嵌套的Serializer
    goods = GoodsSerializer()

    class Meta:
        model = UserFav
        fields = ("goods", "id")
예제 #8
0
파일: views.py 프로젝트: yiweiren96/cmstest
    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)
예제 #9
0
파일: serializer.py 프로젝트: kcxg/MxShop
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__"
예제 #10
0
class OrderGoodsSerializer(serializers.ModelSerializer):
    # 外键关系 所以使用False
    goods = GoodsSerializer(many=False)

    class Meta:
        model = OrderGoods
        fields = '__all__'
예제 #11
0
파일: views.py 프로젝트: zdy456/cms
    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)
예제 #12
0
class OrderGoodsSerializer(serializers.ModelSerializer):
    # odergood 的 外键 goods,非多的那一头
    goods = GoodsSerializer()

    class Meta:
        model = OrderGoods
        fields = '__all__'
예제 #13
0
class UserShoppingCartDeatilSerializer(serializers.ModelSerializer):
    # 外键 ,一个购物车商品记录只能对应一件商品
    goods = GoodsSerializer(read_only=True)

    class Meta:
        model = ShoppingCart
        fields = ("goods", "nums")
예제 #14
0
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
예제 #15
0
class OrderGoodsSerializer(serializers.ModelSerializer):
    #订单商品和商品简历一对一的关系
    goods = GoodsSerializer(many=False)

    class Meta:
        model = OrderGoods
        fields = "__all__"
예제 #16
0
파일: views.py 프로젝트: ZHJlYW0/DT_CMS
 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)
예제 #17
0
    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
예제 #18
0
class UserFavDetailSerializer(serializers.ModelSerializer):

    goods = GoodsSerializer()

    class Meta:
        model = UserFav
        fields = ('id', 'goods')
예제 #19
0
class OrderGoodsSerializer(serializers.ModelSerializer):
    # order 是model的 order的related_name 的名字
    order = GoodsSerializer(many=False)

    class Meta:
        model = OrderGoods
        fields = "__all__"
예제 #20
0
class OrderGoodsSerializer(serializers.ModelSerializer):
    # 订单商品和商品的关系--一对一关系,序列化只需要一条
    goods = GoodsSerializer(many=False)

    class Meta:
        model = OrderGoods
        fields = "__all__"
예제 #21
0
class UserFavDetailSerializer(serializers.ModelSerializer):
    # 通过good_id拿到商品信息,需要嵌套使用
    goods = GoodsSerializer()

    class Meta:
        model = UserFav
        fields = ('goods', 'id')
예제 #22
0
class ShopCartDetailSerializer(serializers.ModelSerializer):
    # 一条购物车关系记录对应的只有一个goods。
    goods = GoodsSerializer(many=False, read_only=True)

    class Meta:
        model = ShoppingCart
        fields = ("goods", "nums")
예제 #23
0
class CartSerializer(serializers.ModelSerializer):

    c_goods = GoodsSerializer()

    class Meta:
        model = Cart
        fields = '__all__'
예제 #24
0
class UserFavDetailSerializer(serializers.ModelSerializer):

    goods = GoodsSerializer()

    class Meta:
        model = UserFav
        fields = ("goods", "id")
예제 #25
0
class OrderGoodsSerializer(serializers.ModelSerializer):

    o_goods = GoodsSerializer()

    class Meta:
        model = OrderGoods
        fields = '__all__'
예제 #26
0
class OrderGoodsSerializer(serializers.ModelSerializer):
    # 这个字段可不唯一,所以many=False
    goods = GoodsSerializer(many=False)

    class Meta:
        model = OrderGoods
        fields = "__all__"
예제 #27
0
class UserFavDetailSerializer(serializers.ModelSerializer):
    # goods = GoodsSerializer(many=True)   #设置goods嵌套序列
    goods = GoodsSerializer()  # 设置goods嵌套序列,goods是个外键,不是对应多个,而是对应一个实例

    class Meta:
        model = UserFav  #model为UserFav
        fields = ("goods", "id")  #设置fields
예제 #28
0
class ShopCartDetailSerializer(serializers.ModelSerializer):
    # 设置动态serializer
    goods = GoodsSerializer(many=False)

    class Meta:
        model = ShoppingCart
        fields = '__all__'
예제 #29
0
 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)
예제 #30
0
class UserFavDetailSerializer(serializers.ModelSerializer):
    #fav对应商品 他自己就是外键 对应的就是一个对象
    goods = GoodsSerializer()

    class Meta:
        model = UserFav
        fields = ("goods", "id")