A Django app to make simple calculations in your django forms
Project description
django-calculation
Make simple calculations in your django forms using django-calculation
. This app provide a Django Widget that derives its value from a expression defined in the widget instance.
The field is updated when any of the source fields change.
Installation
pip install django-calculation
Add calculation
to your INSTALLED_APPS
INSTALLED_APPS = [
...
'calculation',
]
Usage
Import calculation
and complete the definition. The widget NumericCalculationInput
expects the calculation definition as the first argument.
Example
from django import forms
import calculation
class TestForm(forms.Form):
quantity = forms.DecimalField()
price = forms.DecimalField()
amount = forms.DecimalField(
widget=calculation.NumericCalculationInput(
{
'mode': calculation.FORMULA,
'formula': 'quantity*price' #<-- using single math expresion.
},
attrs = {'disabled': True}
)
)
tax = forms.DecimalField(
widget=calculation.NumericCalculationInput(
{
'mode': calculation.FORMULA,
'formula': 'parseFloat(amount/11).toFixed(2)' #<-- using math expression and javascript functions.
},
attrs = {'disabled': True}
)
)
Modes
Currently the app support two modes of calculation FORMULA
and SUMMARY
.
FORMULA
The field value derive from a formula expression. In the expression you can refer to the form field using its name.
amount = forms.DecimalField(
widget=calculation.NumericCalculationInput(
{
'mode': calculation.FORMULA,
'formula': 'quantity*price'
}
)
SUMMARY
The field value derive from a summary definition, it is useful when you need to get the sum of a django formset field.
total = forms.DecimalField(
widget=calculation.NumericCalculationInput(
{
'mode': calculation.SUMMARY,
'summaryFunction': calculation.SUM,
'summaryField': 'amount'
}
)
Summary example
Summary definition in OrderForm
class OrderForm(forms.ModelForm):
total = forms.DecimalField(
widget=calculation.NumericCalculationInput(
{
'mode': calculation.SUMMARY,
'summaryFunction': calculation.SUM,
# subtotal field is in OrderDetForm, subtotal also is a calculated field
'summaryField': 'subtotal'
},
attrs={'disabled': True}
)
)
class Meta:
model = Order
fields = ['date', 'customer']
OrderDetForm
also contain a calculated field subtotal
.
class OrderDetForm(forms.ModelForm):
subtotal = forms.DecimalField(
widget=calculation.NumericCalculationInput(
{
'mode': calculation.FORMULA,
'formula': 'quantity*price'
},
attrs = {'disabled': True}
)
)
class Meta:
model = OrderDet
fields = ['product', 'price', 'quantity', 'subtotal']
# formset definition
OrderDetFormSet = forms.inlineformset_factory(Order, OrderDet, OrderDetForm)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django_calculation-0.0.7-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ac89d43ec6e049910d2ccd3f2f23900694a7d7117c00049f8ffa35cc7336660b |
|
MD5 | fd819d5b3be613cade32035b72b0f3f9 |
|
BLAKE2b-256 | 3e9126ea2a6c6faf98f45bf81310dedf79f686507c4a2166d8cec10c1bf2d621 |