autoupdated database fields for model methods
Project description
django-computedfields
django-computedfields provides autoupdated database fields for model methods.
Tested with Django 2.2 and 3.0 (Python 3.6 to 3.8).
Example
Just derive your model from ComputedFieldsModel
and place
the @computed
decorator at a method:
from django.db import models
from computedfields.models import ComputedFieldsModel, computed
class MyModel(ComputedFieldsModel):
name = models.CharField(max_length=32)
@computed(models.CharField(max_length=32), depends=[['self', ['name']]])
def computed_field(self):
return self.name.upper()
computed_field
will be turned into a real database field
and can be accessed and searched like any other database field.
During saving the associated method gets called and it’s result
written to the database. With the method compute('fieldname')
you can inspect the value that will be written, which is useful
if you have pending changes:
>>> person = MyModel(forename='berty')
>>> person.computed_field # empty since not saved yet
>>> person.compute('computed_field') # outputs 'BERTY'
>>> person.save()
>>> person.computed_field # outputs 'BERTY'
The depends
keyword argument can be used with any relation to indicate dependencies to fields on other models as well:
from django.db import models
from computedfields.models import ComputedFieldsModel, computed
class MyModel(ComputedFieldsModel):
name = models.CharField(max_length=32)
fk = models.ForeignKey(SomeModel)
@computed(models.CharField(max_length=32), depends=[['self', ['name']], ['fk', ['fieldname']]])
def computed_field(self):
return self.name.upper() + self.fk.fieldname
Now changes to self.name
or fk.fieldname
will update computed_field
.
NOTE: The old depends syntax is deprecated and should be replaced with the new syntax.
It also should contain a self
entry with local fields to get reliable updates from local field changes.
This is especially true for advanced usage with save(update_fields=[...])
or bulk actions.
The old syntax will be removed with a future version.
Documentation
The documentation can be found here.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Hashes for django-computedfields-0.0.18.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 25792ed1a65c0bc2907330620d6fa733c14cadcd6d3959c36814a1d1157260b7 |
|
MD5 | 4747d04b7b9b2074b45da619e5a1d5ac |
|
BLAKE2b-256 | 443fde5f9a321e5117bb415008030de38b540c509bd2dfed74f09edaea708a99 |