Beispiel #1
0
def get_shop_data(reqeust):
    result = {}
    # 保存分类信息的数据
    li = []
    try:
        # 查询分类信息表
        # SELECT *  FROM  category
        cates = Category.objects.all()

        for cate in cates:
            # Product.objects.filter(cid=cate.id)
            # 查询每个分类的商品信息
            products = cate.product_set.all()
            # 遍历商品信息  通过商品对象来获取图片的信息
            for product in products:
                # 商品跟图片表之前的关系是一对多的关系
                # SELECT  * FROM  PRODUCTIMAGE WHERE PID=PRODUCT.ID
                product.imgs = BaseModel.qs_to_dict(
                    product.product_image.all())

            # 商品信息添加到每个分类对象里
            cate.products = BaseModel.qs_to_dict(products)
            li.append(cate.to_dict())
        result.update(state=200, msg='success', data=li)
    except:
        result.update(state=200, msg='查询失败')

    return HttpResponse(json.dumps(result), content_type='Application/json')
Beispiel #2
0
def get_shop_data(request):
    result = {}
    #保存分类信息的数据
    li = []
    try:
        #查看所有的category内容
        cates = Category.objects.all()
        for cate in cates:
            #Product.objects.filter(cid = category)
            #查询每个分类的商品信息
            products = cate.product_set.all()
            #遍历商品信息,通过商品对象来获取图片的信息
            for product in products:
                #product和图片表之间是一对多的关系
                #select * from productimage where pid=product.id
                #product新增一个属性aaa或者imgs(值得研究)
                product.imgs = BaseModel.qs_to_dict(
                    product.product_image.all())
            #将商品信息添加到每个分类对象
            cate.products = BaseModel.qs_to_dict(products)
            #cate.to_dict()将python对象转换成为字典
            li.append(cate.to_dict())
            #data列表
        result.update(state=200, msg='success', data=li)
    except:
        result.update(state=404, msg='失败')
    return HttpResponse(json.dumps(result), content_type='Application/json')
Beispiel #3
0
def get_shop_data(request):
    result = {}
    li = []
    try:
        cates = Category.objects.all()

        for cate in cates:
            products = cate.product_set.all()
            for product in products:
                product.imgs = BaseModel.qs_to_dict(
                    product.product_image.all())
            cate.products = BaseModel.qs_to_dict(products)
            li.append(cate.to_dict())
        result.update(state=200, msg='success', data=li)
    except:
        result.update(state=-1, msg='失败')
    return HttpResponse(json.dumps(result), content_type='Application/json')
Beispiel #4
0
def shop_car_list(request):
    uid = request.GET.get('uid')
    if uid:
        try:
            cars = ShopCar.objects.filter(Q(uid=uid) & Q(status=1))
            for car in cars:
                car.product = car.pid.to_dict()
            return response_json(data=BaseModel.qs_to_dict(cars))
        except Exception as e:
            return response_json(status=400, msg='查询失败')
    else:
        return response_json(status=400, msg='参数错误')
Beispiel #5
0
def get_shop_detail(request):
    result = {}
    pid = request.GET.get('pid')
    try:
        product = Product.objects.get(id=pid)
        # 获取详情图片数据
        img_queryset = product.product_image.filter(type='type_single')
        product.imgs = BaseModel.qs_to_dict(img_queryset)
        # Review.objects.filter(pid=pid).count()
        # 评论数据
        product.comment_count = product.review_set.count()
        # 获取产品的销售量
        # product.orderitem_set.aggregate(count=Sum('number'))  字典
        number_dict = product.orderitem_set.aggregate(sum=Sum('number'))
        product.sale_count = number_dict.get('sum')
        result.update(state=200, msg='success', data=product.to_dict())
    except Exception as e:
        print(e)
        # -2 表示商品信息不存在
        result.update(state=-2, msg='查询失败')
    return HttpResponse(json.dumps(result), content_type='Application/json')