根据单独的视图检查对象的属性 (Django|Django-Rest-Framework)

Checking properties of an object based on separete view (Django|Django-Rest-Framework)

我正在使用 django-rest-framework 和 React.js。我需要在后端有一个单独的函数来检查 CartItem.amount 是否低于 ProductStock.quantity,如果 CartItem.product 等于 ProductStock.id 当然(这部分不起作用)。对于相同的功能,我需要检查定价是否与 Product 模型中的定价相同,但令人惊讶的是,这部分功能有效。我该怎么做才能确保 CartItem.amount 在高于 ProductStock.quantity 时会降低?

代码如下:

Product 是其余模型的蓝图。

ProductStock 跟踪所有产品的不同尺寸数量。

CartItem 是一个用于跟踪用户购买了多少产品的模型。

models.py

class Product(models.Model):
    name = models.CharField(max_length=150)
    price = models.IntegerField()
    slug = models.SlugField(blank=True, null=True)

    def __str__(self):
        return self.name

    def save(self, *args, **kwargs):
        self.slug = slugify(self.name)
        super(Product, self).save()

class ProductStock(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    size = models.CharField(max_length=6)
    quantity = models.IntegerField(default=0)

    def __str__(self):
        return f'{self.quantity} x {self.product.name}: {self.size}'

class CartItem(models.Model):
    user = models.ForeignKey(CustomUser, on_delete=models.CASCADE)
    product = models.ForeignKey(ProductStock, on_delete=models.CASCADE, blank=True, null=True)
    amount = models.IntegerField(default=0, blank=True, null=True)
    size = models.CharField(max_length=10, blank=True, null=True)
    price = models.IntegerField(blank=True, null=True)

    def __str__(self):
        return f'{self.user.email}, {self.product.product.name}, {self.product.size}: {self.amount}'
    
    def save(self, *args, **kwargs):
        if self.amount > self.product.quantity:
            self.amount = self.product.quantity
        self.size = self.product.size
        self.price = self.product.product.price
        super(CartItem, self).save()

views.py

def check_cart_content(request,user_id):
    cart = CartItem.objects.filter(user=user_id)

    for item in cart:
        product_stock = ProductStock.objects.filter(product=item.product.id)
        for stock in product_stock:
            if item.amount > stock.quantity:
                item.amount = stock.quantity

            product = Product.objects.get(id=item.product.id)
            if item.price != product.price:
                item.price = product.price

            item.save()

            return JsonResponse({'success': 'Cart checked'})

您需要更改 JsonResponse 的缩进,因为它在 product_stock 中的第一个 stock 之后结束。另外 item.save() 更可能需要更改缩进。修改干净 Python.

def check_cart_content(request,user_id):
    cart = CartItem.objects.filter(user=user_id)

    for item in cart:
        product_stock = ProductStock.objects.filter(product=item.product.id)
        for stock in product_stock:
            if item.amount > stock.quantity:
                item.amount = stock.quantity

        product = Product.objects.get(id=item.product.id)
        if item.price != product.price:
            item.price = product.price

        item.save()

    return JsonResponse({'success': 'Cart checked'})

您应该考虑重建模型树,因为 ProductStockForeignKey 似乎是不好的解决方案。最好在 Product 的函数中计数,类似于:

def quantity(self):
    return Product.objects.all().count()

因为它是动态工作的,您不需要数据库中的额外对象。