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.
Example
Using FormulaInput
widget
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.
<form method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit">
</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.
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.
total = forms.DecimalField(
widget=calculation.SummaryInput(
function=calculation.SUM,
field='amount'
)
Summary example
Summary definition in OrderForm
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
.
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.
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
File details
Details for the file django-calculation-1.0.0.tar.gz
.
File metadata
- Download URL: django-calculation-1.0.0.tar.gz
- Upload date:
- Size: 9.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 125136fdb06ca6ed118843f1fdf354ffd4554dfa0d67f684a6c86a8e5c07016d |
|
MD5 | 0d483c23e3bb815f5a3befcd834784ce |
|
BLAKE2b-256 | a0036b5b04e13fa7386cbc3ba64af53a5030a22d771fca51c506c5a3e0c260d5 |
File details
Details for the file django_calculation-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: django_calculation-1.0.0-py3-none-any.whl
- Upload date:
- Size: 8.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7d6144aae369c49b8e3e7011a491948d906df383ad7dedf5b0d1d0b5ff0cb0fa |
|
MD5 | 8501efcfec81db7cc2af0b884b29b78b |
|
BLAKE2b-256 | 6b00dc55b77288fac344162da7ac15a233d2c01bced0e538f808c1191e7bc91a |