In-database calculations for Django
Switch branches/tags
Nothing to show
Pull request Compare This branch is even with joshbohde:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

Django DB Calculations


This is a way for doing calculations in the database, and having them magically appear as attributes on your models.


Django's extra isn't DRY.


You'll need Django 1.2. Add the custom manager to your model, like so

from django.db import models
from db_calculations import CalculationManager

class MyModel(models.Model):
    objects = CalculationManager()

Now you can use the calculate method, using the builtin F objects to build your expression


>>> from basic.models import Number
>>> from django.models import F
>>> Number.objects.create(x=2, y=3)
>>> Number.objects.calculate(z=F('x')+F('y'))[0].z
... 5.0

>>> Number.objects.calculate(z=F('x')-F('y'))[0].z
... -1.0

>>> Number.objects.calculate(z=F('x')*F('y'))[0].z
... 6.0

>>> Number.objects.calculate(z=F('x')/F('y'))[0].z
... 0.66666666666666663

>>> Number.objects.calculate(z=F('x')+F('y')).calculate(z_squared=F('z')*F('z'))[0].z_squared
... 25.0


The current state is more of a proof of concept than any sort of thing you might want to use for your project. It pretty much won't work outside of basic operations. In fact, it will go pretty crazy once you start to do anything fancy.