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)
CodePudding user response:
You're not saving the emplacement object.
Try this:
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
emplacement.save()
i =1
product.save()
return redirect('print-barcode', product.id)
else:
form = ValidateProductForm(instance=product)
Edit
Your Product model indicates that each Product has only ONE Emplacement, since that's where the ForeignKey is. But, a single Emplacement, can have MANY Product objects. ForeignKey is a ManyToOne relationship. When iterating through ALL Emplacements, you are using the SAME Product, and that's why they are all getting the same number in your admin. I'm guessing, but perhaps what you're after is to find the SINGLE appropriate Emplacement model that the specific Product object has, nad then update just that one. In other words, NO iterating at all.
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
# Here are my proposed changes:
product.save() # Update the product
emplacement = product.emplacement # Get the correct Emplacement
emplacement.volume -= product.volume # Adjust the volume
emplacement.save() # Save the emplacement
return redirect('print-barcode', product.id)
else:
form = ValidateProductForm(instance=product)