Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Calculation of input value and store it into database django

I want per unit price value according to input value and save it on database, I don’t know how to do this task kindly help me plz

model.py

class Celldetail(models.Model):
    CellInvoiceNo = models.IntegerField(null=True, blank=True)
    Cell_Type = models.CharField(max_length=200, null=True)
    Cell_qty = models.DecimalField(default=0)
    Cell_price = models.DecimalField(default=0)
    Cell_PUP = models.FloatField()

views.py

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

def add_inventory_view(request):
    form = CellForm(request.POST or None)
    if form.is_valid():
        form.save()
    context = {
        'form': form,
    }
    return render(request, "addinventory.html", context)

>Solution :

Override the save() function of your model:

class CellDetail(models.Model): # note Pascal case
   ...

   def save(self, *args, **kwargs):
      self.Cell_PUP = self.Cell_price / self.Cell_qty
      super(CellDetail, self).save(*args, **kwargs)

I would recommend renaming your fields to be clearer, remove the unnecessary prefix, and to use snake case, e.g.

invoice_no = ...
type = ...
quantity = ...
price = ...
per_unit_price = ...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading