i'm buildint a warehouse management application, i have a product model and an placement for each product, every placement has a volume, once i put a product in a placement, the volume of this placement must be reduced. The problem is when the app finds a placement for the product, the placement volume stay the same
models.py
class Emplacement(models.Model):
address = models.CharField(max_length=25, blank=True)
volume = models.DecimalField(max_digits=10, decimal_places=2, null=True)
class Product(models.Model):
name = models.CharField(max_length=100)
quantity = models.PositiveIntegerField()
is_disponible = models.BooleanField(default=False)
volume = models.DecimalField(max_digits=20, decimal_places=2, null=True)
emplacement = models.ForeignKey(Emplacement, on_delete=models.CASCADE, null=True)
views.py
def product_detail(request, pk):
product = get_object_or_404(Product, id=pk)
if request.method == 'POST':
form = ValidateProductForm(request.POST, instance=product)
if form.is_valid():
product = form.save(commit=False)
product.volume = form.cleaned_data['longueur'] * form.cleaned_data['largeur'] * form.cleaned_data['hauteur']
product.is_disponible = True
all_emplacements = Emplacement.objects.all()
i=1
while i <= product.quantity:
for emplacement in all_emplacements:
if product.volume < emplacement.volume:
product.emplacement = emplacement
emplacement.volume -= product.volume
i+=1
product.save()
return redirect('print-barcode', product.id)
else:
form = ValidateProductForm(instance=product)
context = {
'product': product,
'form': form,
}
return render(request, 'dashboard/product_detail.html', context)