class UserDetailSerilizer(serializers.ModelSerializer): """ 用户收藏内容 """ goods = GoodsSerializer() class Meta: model = UserFav fields = ('goods',)
class UserFavListSerializer(serializers.ModelSerializer): """ 用于查看用户收藏 """ goods = GoodsSerializer() class Meta: model = UserFav fields = ("goods", "id")
class UserFavDetailSerializer(serializers.ModelSerializer): ''' 用户收藏详情 ''' #通过商品id获取收藏的商品,需要嵌套商品的序列化 goods = GoodsSerializer() class Meta: model = UserFav fields = ('goods', 'id')
class ShoppingCartDetailSerializer(serializers.ModelSerializer): """ 购物车 """ user = serializers.HiddenField(default=serializers.CurrentUserDefault()) nums = serializers.IntegerField() goods = GoodsSerializer() class Meta: model = ShoppingCart fields = '__all__'
class OrderDetailSerializer(serializers.ModelSerializer): """ 订单商品 """ goods = GoodsSerializer() alipay_url = serializers.SerializerMethodField(read_only=True) def get_alipay_url(self, obj): aplipay = AliPay(appid=ALIPAY['APPID'], app_notify_url=ALIPAY['NOTIFY_URL'], return_url=ALIPAY['RETURN_URL'], debug=ALIPAY['DEBUG'], app_private_key_path=ALIPAY['APP_PRIVATE_KEY'], alipay_public_key_path=ALIPAY['ALIPAY_PUBLIC_KEY']) url = aplipay.direct_pay(subject=obj.order_sn, out_trade_no=obj.order_sn, total_amount=obj.order_mount) ret_url = ALIPAY['ALIPAY_URL'] + '?{data}'.format(data=url) return ret_url class Meta: model = OrderGoods fields = '__all__'
class UserFavDetailSerializer(serializers.ModelSerializer): goods = GoodsSerializer() class Meta: model = UserFav fields = ("goods", "id")
class OrderGoodsSerializer(serializers.ModelSerializer): goods = GoodsSerializer(many=False) class Meta: model = OrderGoods fields = "__all__"
class ShoppingCartDetailSerializer(serializers.ModelSerializer): goods = GoodsSerializer(many=False) class Meta: model = ShoppingCart fields = "__all__"
class OrderGoodsDetailSerializer(serializers.ModelSerializer): goods = GoodsSerializer() class Meta: model = OrderGoods fields = "__all__"
def get(self, request, format=None): goods = Goods.objects.all()[:10] goods_serializer = GoodsSerializer(goods, many=True) # 因为是goods是list,多条数据要增加many=True return Response(goods_serializer.data)
class UserFavDetailSerializer(serializers.ModelSerializer): goods = GoodsSerializer() class Meta: model = UserFav fields = ('goods', 'id')
class UserFavDetailSerializer(serializers.ModelSerializer): goods = GoodsSerializer() class Meta: model = UserFav fields = "__all__"