Add the shape to the model. Django



  • Just started studying Django. There's a uniform. I want one of her fields to get a full meaning and add this value to one of the attributes of the existing model.Product) Here are models:

    class Product(models.Model):
        class Meta():
            db_table = 'Product'
    
    idModel = models.ForeignKey(SkiModel)
    growth_from = models.IntegerField()
    growth_to = models.IntegerField()
    weight_from = models.IntegerField()
    weight_to = models.IntegerField()
    cost = models.IntegerField()
    quantity_in_stock = models.IntegerField(default=0)
    def __str__(self):
        return str(self.id)
    

    class Delivery(models.Model):
    class Meta():
    db_table = 'Delivery'

    Product = models.ForeignKey(Product)
    Amount = models.IntegerField()
    Receipt_date = models.DateField()
    User = models.ForeignKey(User)
    

    Here's the shape:

    class DeliveryForm(ModelForm):
    class Meta():
    model = Delivery
    fields = 'Product', 'Amount', 'Receipt_date'

    Here's the function. views.py:

    def add_delivery(request, user_id):
    if request.POST:
    form = DeliveryForm(request.POST)
    if form.is_valid():
    Delivery = form.save(commit=False)
    Delivery.User = User.objects.get(id=user_id)
    form.save()
    Product.objects.get(id=int(form['Product'].value())).quantity_in_stock += int(form['Amount'].value())
    return redirect('/Delivery/')

    Not adding. Thank you.



  • Modified records must be recorded:

    pr = Product.objects.get(id=int(request.POST.get('Product')))
    pr.quantity_in_stock += int(request.POST.get('Amount'))
    pr.save()
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2