Skip to main content

A Django app to make simple calculations in your django forms

Project description

django-calculation

Release PyPI version PyPI - Django Version PyPI - Python Version

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.

calculation

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)

chrome-capture

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

django-calculation-1.0.0.tar.gz (9.2 kB view hashes)

Uploaded Source

Built Distribution

django_calculation-1.0.0-py3-none-any.whl (8.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page