编辑后,库存数量没有增加,库存总数量也没有减少,我希望它应该在 django 中更新

After edit the quantity is not increasing from stocks as well as not decreasing from the total stocks quantity, I want it should be updated in django

提问人:Shivang Kant 提问时间:11/17/2023 最后编辑:Shivang Kant 更新时间:11/17/2023 访问量:18

问:

当我编辑数据时,数量没有更新,它没有减少,也没有在数据库中增加,我希望当我编辑数据时,如果数量比以前多,旧数量和新数量之间的差额将被添加,如果旧新数量之间的差额小于它将减少......请帮帮我 请帮帮我

models.py

# models.py
from collections.abc import Collection
from django.contrib.auth.models import User
from django.db import models
from django.core.exceptions import ValidationError

class Client(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    mobile = models.CharField(max_length=15)
    address = models.CharField(max_length=255)

    def clean_mobile(self):
        mobile=self.mobile
        if len(str(mobile))!= 10:
            raise ValidationError("invalid mobile number")
        return mobile


 class Supplier(models.Model):
     
    
    user = models.ForeignKey(User, on_delete=models.CASCADE)
        product_name = models.CharField(max_length=255)
        product_price = models.DecimalField(max_digits=10, 
        decimal_places=2)
        image = models.ImageField(upload_to='products/')
        quantity= models.PositiveIntegerField(default=1)
        category=models.CharField(choices=CHOICES3,max_length=10)
        
        def __str__(self):
            return self.product_name

CHOICES3 = [('Tshirt','Tshirt'),('Jeans','Jeans'),('Jacket','Jacket'),('Shoes','Shoes'),('Watch','Watch')]
CHOICES2 = [('Polo-Tshirt','Polo-Tshirt'),('Levis-Jeans','Levis-Jeans'),('Denim-Jacket','Denim-Jacket'),('Puma-Shoes','Puma-Shoes'),('Titan-Watch','Titan-Watch')]


class Stocks(models.Model):
    product_name = models.CharField(choices=CHOICES2,max_length=100)
    quantity = models.PositiveBigIntegerField(default=100)


    def __str__(self):
         return self.product_name


views.py

def supplier_dashboard(request):
    if request.method == 'POST':
        product_name = request.POST.get('product_name')
        product_price = request.POST.get('product_price')
        image = request.FILES.get('image')
        quantity = request.POST.get('quantity')
        category = request.POST.get('category')
        data = Supplier.objects.create(user=request.user, category=category, product_name=product_name, product_price=product_price, image=image, quantity=quantity)
        data.save()
        supplier = Supplier.objects.filter(user=request.user)
        return HttpResponseRedirect(reverse('update_stocks', args=[data.id]))
    else:
        supplier = Supplier.objects.filter(user=request.user)
    return render(request, 'enroll/supplier_dashboard.html', {'suppliers': supplier})




supplier_edit.view

def supplier_edit(request,id):
    try:
        product=Supplier.objects.get(id=id)
        if product.user == request.user:
            if request.method=='POST':
                    product.product_name = request.POST.get('product_name')
                    product.product_price = request.POST.get('product_price')
                    if 'image' in request.FILES:
                        product.image=request.FILES.get('image')
                    product.quantity = request.POST.get('quantity')
                    product.category = request.POST.get('category')
                    product.save()
                    messages.success(request,"ProDuct Updated Succesfully")
                    return redirect('supplier_dashboard')
            return render(request,'enroll/supplier_edit.html',{'products':product})
    except Exception as e :
        messages.error(request," Some Error Occured ",e)     
        return render(request,'enroll/supplier_edit.html',{'products':product})

def update_stocks(request, id):
    print("------------", id)
    product = Supplier.objects.get(id=id)
    stocks = Stocks.objects.get(product_name=product.product_name)
    print(product.quantity, stocks.quantity)
    stocks.quantity = stocks.quantity - product.quantity
    stocks.save()
    return HttpResponseRedirect(reverse('supplier_dashboard'))
python django django-views 电子商务 crud

评论


答: 暂无答案