Django rest_framework数量在电子商务项目中没有增加

tcomlyy6  于 2023-03-13  发布在  Go
关注(0)|答案(2)|浏览(118)

models.py

class Cartlist(models.
    user = models.ForeignKey(User, on_delete=models.
    created_at = models.DateField(auto_now_add=True)
    class Meta:
        ordering = ['id']
    def __str__(self):
        return self.user.username
class Items(models.Model):
    cart = models.ForeignKey(Cartlist, on_delete=models.CASCADE)
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    quantity = models.IntegerField(default=1)
    order = models.BooleanField(default=False)
    created_at = models.DateField(auto_now_add=True)

    class Meta:
        ordering = ['id']

    def __str__(self):
        return '{}'.format(self.product)

Views.py

class AddCartView(generics.CreateAPIView):
    permission_classes = [IsAuthenticated]
    queryset =  Items.objects.all()
    serializer_class = AddCartSerializer

Serializer.py

class AddCartSerializer(serializers.ModelSerializer):
    class Meta:
        model = Items
        fields = ['product']

    def create(self, validated_data):
        user = self.context['request'].user 
        product = self.validated_data['product'] 
        if Cartlist.objects.filter(user=user):
            item = Items.objects.filter(cart__user=user,product=product)
            if item:
                item.quantity += 1
                item.save()
                return item
            else:
                item = Items.objects.create(cart=Cartlist.objects.get(user=user),**validated_data)
                item.save()
                return item
        
        else:
            cart = Cartlist.objects.create(user=user)
            cart.save()
            item = Items.objects.create(cart=cart,**validated_data)
            item.save()
            return item

Exception Value:'QuerySet' object has no attribute 'quantity'
如果产品已存在,则将数量字段增加1。默认值为1。如果用户将相同产品添加到其购物车列表中,则产品数量必须增加1

ws51t4hk

ws51t4hk1#

item = Items.objects.filter(cart__user=user,product=product)
if item:
    item.quantity += 1
    item.save()
    return item

filter返回Queryset个对象(对象列表),即使查询结果中只有一个对象。换句话说,filter返回items,但不返回item
如果你想得到单个项目,你必须使用下面的结构之一:

  • Items.objects.get(cart__user=user,product=product)(文件)
  • Items.objects.filter(cart__user=user,product=product).first()(文档)
e1xvtsh3

e1xvtsh32#

此代码不起作用,因为您正在尝试从QuerySet中获取数量,而您不应该使用此代码

def create(self, validated_data):
    user = self.context['request'].user 
    product = self.validated_data['product'] 
    if Cartlist.objects.filter(user=user):
        item = Items.objects.filter(cart__user=user,product=product).first()
        if item:
            item.quantity += 1
            item.save()
            return item
        else:
            item = Items.objects.create(cart=Cartlist.objects.get(user=user),**validated_data)
            item.save()
            return item
    
    else:
        cart = Cartlist.objects.create(user=user)
        cart.save()
        item = Items.objects.create(cart=cart,**validated_data)
        item.save()
        return item

或者您也可以使用

def create(self, validated_data):
    user = self.context['request'].user 
    product = self.validated_data['product'] 
    if Cartlist.objects.filter(user=user):
        item = Items.objects.filter(cart__user=user,product=product)
        if item:
            item.update(quantity=F("quantity")) 
            return item
        else:
            item = Items.objects.create(cart=Cartlist.objects.get(user=user),**validated_data)
            item.save()
            return item
    
    else:
        cart = Cartlist.objects.create(user=user)
        cart.save()
        item = Items.objects.create(cart=cart,**validated_data)
        item.save()
        return item

相关问题