0

I'm still learning how to create Inventory Management by using Django2.

I have models that receive user input and save it to the database using Django.

I want to calculate & get the total value from the user who fills up the form. And want to show the result to the user in the templates.html

Clue :

I need to get the exact total price from:

  • total_product * per_unit_price and show the result in: subtotal_unit_price.

  • per_unit_price * profit_precent (in % if possible) and divide by 100 % and then sum / added up with per_unit_price, the result are in the new field which is can be profit_bruto.

  • profit_bruto * per_unit_product and the result will be in: sell_price.

What is the right way to approach this?

I'am stuck with this documentation.

models.py

"Stock Input"
class InputStock(models.Model):
    number_product = models.AutoField(primary_key=True)
    product_name = models.CharField(max_length=30)
    total_product = models.IntegerField()
    per_unit_product = models.IntegerField(blank=True)
    per_unit_price = models.DecimalField(max_digits=14, decimal_places=2)
    subtotal_unit_price = models.DecimalField(max_digits=14, decimal_places=2)
    profit_percent = models.IntegerField()
    sell_price = models.DecimalField(max_digits=14, decimal_places=2)



    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)


    def publish(self):
        self.published_date = timezone.now()
        self.save()

    def __str__(self):
        return self.nama_product

views.py

@login_required()
def InputStock(request):
    form = InputStockForm(request.POST)
    if request.method == 'POST':

        if form.is_valid():
            form.save()
            form = DaftarBarangForm()
    return render(request, 'input_data.html', {'form':form})

forms.py

class InputStockForm(forms.ModelForm):
    class Meta:
        model = InputStock
        fields = '__all__'

How do I approach this?

Currently, the form is only saved the data from (user input) to the database. not calculate 'em.

iColdPlayer
  • 475
  • 5
  • 11
  • Possible duplicate of [How to get value from form field in django framework?](https://stackoverflow.com/questions/4706255/how-to-get-value-from-form-field-in-django-framework) – Zaraki Kenpachi Jul 09 '19 at 07:25

1 Answers1

0

you can put you logic in form.save() function.just like this

class InputStockForm(forms.ModelForm):
    class Meta:
        model = InputStock
        fields = '__all__'

    def save(self, commit=True):
        instance = super().save(commit=False)
        # your calculate
        instance.subtotal_unit_price = instance.total_product * instance.per_unit_price
        profit_bruto  = instance.per_unit_price * instance.profit_precent
        instance. sell_price = profit_bruto * instance.per_unit_product
        if commit:
            instance.save()
        return instance
baskershu
  • 109
  • 6
  • I've tried as your suggestion. but how to define the percent method? for example, the field `profit_percent = models.IntegerField()` it should be percent, how I can make it? – iColdPlayer Jul 10 '19 at 14:13