Skip to main content

django-expression-fields allows typing mathematical expressions into form fields and having only the calculated result stored in the database.

Project description

Introduction

django-expression-fields lets your users type a mathematical expression in a form field. Python does the math and stores the result in the database. For example, suppose you have a model to track Things, like this:

class Thing(models.Model):
cost = models.DecimalField(

max_digits=5, decimal_places=2, null=True, blank=True)

Suppose Things come in packs of 12 for $7.99. Your users have to do some math to fill in the cost of a single Thing, $0.67.

But not with an expression field! Create your form like this:

class ThingForm(forms.Form):
cost = DecimalExpressionField(

max_digits=5, decimal_places=2, required=False)

Now you user can simply type 7.99/12 in the field and Python will do the math for them!

Requirements and Installation

Right now, the project has no dependencies.

  • pip install django-expression-fields

  • Add expression_fields to your INSTALLED_APPS.

Use

from django import forms from expression_fields.fields import DivideDecimalField

class MyForm(forms.Form):
cost = DecimalExpressionField(

max_digits=5, decimal_places=2, required=False)

Tests

./run-tests.sh.

Limitations

Right now there is only the DivideDecimalField which allows a single division sign. It’s a proof of concept for a fuller DecimalExpression field which will allow full expressions. I later intend to build Integer and Float expression fields as well.

Contributions

I built this little project to satisfy a personal need, but thought it might be useful enough for others. If you have contributions, please don’t hesitate to send a PR. Let’s keep the tests passing and all will be well. My personal stack is currently Django 1.8 on Python 3.4, but once I can get some Travis set up, there’s no reason not to support other versions.

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-expression-fields-0.1.1.tar.gz (3.5 kB view details)

Uploaded Source

File details

Details for the file django-expression-fields-0.1.1.tar.gz.

File metadata

File hashes

Hashes for django-expression-fields-0.1.1.tar.gz
Algorithm Hash digest
SHA256 67dc845ce5bca042ddd802fc413a38d4b376bbd2d8a0949db7b685a17a9b7a63
MD5 1fbaa90d73f25868ec61f109cdd9a4ce
BLAKE2b-256 ddeec079ba126fc7ee73325f53cf5c39559a4a87960788a0b067ce3de4ca18a4

See more details on using hashes here.

Supported by

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