Computed Property Fields for Django
Inspired by Google Cloud Datastore NDB Computed Properties.
Automatically store a computed value in the database when saving your model so you can filter on values requiring complex (or simple!) calculations.
-
Install this library
pip install django-computed-property
-
Add to
INSTALLED_APPS
INSTALLED_APPS = [ ..., 'computed_property' ]
-
Add a computed field to your model(s)
from django.db import models import computed_property class MyModel(models.Model): doubled = computed_property.ComputedIntegerField(compute_from='double_it') base = models.IntegerField() def double_it(self): return self.base * 2
Documentation available at http://django-computed-property.readthedocs.io/en/latest/