https://github.com/blasferna/django-calculation
Make simple calculations in your django forms.
https://github.com/blasferna/django-calculation
django django-formsets django-widget javascript python
Last synced: 5 months ago
JSON representation
Make simple calculations in your django forms.
- Host: GitHub
- URL: https://github.com/blasferna/django-calculation
- Owner: blasferna
- License: mit
- Created: 2021-08-10T14:33:38.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2022-06-07T17:25:24.000Z (about 4 years ago)
- Last Synced: 2025-09-27T17:22:25.416Z (9 months ago)
- Topics: django, django-formsets, django-widget, javascript, python
- Language: JavaScript
- Homepage:
- Size: 46.9 KB
- Stars: 21
- Watchers: 3
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# django-calculation
[](https://github.com/blasferna/django-calculation/actions/workflows/release.yml)
[](https://pypi.python.org/pypi/django-calculation)
 
Make simple calculations in your django forms using `django-calculation`. This app provide a **[Django Widget](https://docs.djangoproject.com/en/3.2/ref/forms/widgets/)** that derives its value from a expression defined in the widget instance.
The field is updated when any of the source fields change.

## Installation
```bash
pip install django-calculation
```
### Add `calculation` to your INSTALLED_APPS
```python
INSTALLED_APPS = [
...
'calculation',
]
````
## Usage
Import `calculation` and complete the definition.
### Example
Using `FormulaInput` widget
```python
from django import forms
import calculation
class TestForm(forms.Form):
quantity = forms.DecimalField()
price = forms.DecimalField()
amount = forms.DecimalField(
widget=calculation.FormulaInput('quantity*price') # <- using single math expression
)
apply_taxes = forms.BooleanField(initial=True)
tax = forms.DecimalField(
# using math expression and javascript functions.
widget=calculation.FormulaInput('apply_taxes ? parseFloat(amount/11).toFixed(2) : 0.0')
)
```
`django-calculation` works with static files and therefore it is necessary to include the media of the form in the template file.
```html
{% csrf_token %}
{{ form }}
{{ form.media }}
```
### 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.
```python
amount = forms.DecimalField(
widget=calculation.FormulaInput('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.
```python
total = forms.DecimalField(
widget=calculation.SummaryInput(
function=calculation.SUM,
field='amount'
)
```
#### Summary example
Summary definition in `OrderForm`
```python
class OrderForm(forms.ModelForm):
total = forms.DecimalField(
# using SumInput a SummaryInput abstraction
widget=calculation.SumInput('subtotal')
)
class Meta:
model = Order
fields = ['date', 'customer']
```
`OrderDetForm` also contain a calculated field `subtotal`.
```python
class OrderDetForm(forms.ModelForm):
subtotal = forms.DecimalField(
widget=calculation.FormulaInput('quantity*price')
)
class Meta:
model = OrderDet
fields = ['product', 'price', 'quantity', 'subtotal']
# formset definition
OrderDetFormSet = forms.inlineformset_factory(Order, OrderDet, OrderDetForm)
```

## Roadmap
- Create demo project.
- Create documentation.
- Add changelog.