Skip to main content

Distrubited calculations for Django models

Project description

Django Gem

Django-gem is a reusable Django package that allows user to offload on-the-fly calculations for Django models by putting them into database and providing and easy to use architecture to define, how and when those database fields need to be refreshed.

Installation


Using pip

pip install django-gem

Using poetry

poetry add django-gem

Then add 'django_gem' to your INSTALLED_APPS.

INSTALLED_APPS = [
    ...
    'django_gem',
]

Usage

First you will need to create a class that knows how to re-calculate your database fields. In this package those classes are called "Cutters".

Each of the methods on the cutter need to be defined as property, and the names of those methods should mirror re-calculated fields on the Gem model in the format of cut_<field_name>.

Side effects on the properties define what related models should trigger recalculations on those fields.

from django_gems_example.gems.callbacks.author import AuthorCutterCallbacks
from django_gems_example.models import Author, Book

from django_gem.cutters.base import BaseCutter
from django_gem.decorators.cutters import side_effects


class AuthorCutter(BaseCutter):
    instance: Author

    @property
    @side_effects(
        (Author, [], AuthorCutterCallbacks.author),
        (Book, ["author"], AuthorCutterCallbacks.book_author),
    )
    def cut_book_count(self):
        return self.instance.books.count()

Django-gem allows you to create a "satellite" model for you main one, and define the CutterEngineMeta with the created cutter.

from django.db import models
from django_gems_example.gems.cutters.author import AuthorCutter
from django_gems_example.models import Author

from django_gem.models.base import CutterEngineBaseModel, CutterEngineMetaBase


class AuthorGem(CutterEngineBaseModel):
    book_count = models.IntegerField(default=0)

    class CutterEngineMeta(CutterEngineMetaBase):
        model = Author
        cutter = AuthorCutter

To access these fields from the main model, you need to define a property on the model with the same name as the Gem field name, and wrap it with a gem_property decorator.

from django.db import models

from django_gem.decorators import gem_property


class Author(models.Model):
    name = models.CharField(max_length=100)

    @gem_property
    def book_count(self):
        return self.gem.book_count

All the calculations happen implicitly, as cutter_registry that is populated on Django app startup loads all the related models and attaches hooks for calculation. But, if needed, there are methods that you can invoke directly from the Saw class. It allows refreshing a model, a queryset or an entire content type.

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_gem-1.1.0.tar.gz (16.9 kB view details)

Uploaded Source

Built Distribution

django_gem-1.1.0-py3-none-any.whl (30.6 kB view details)

Uploaded Python 3

File details

Details for the file django_gem-1.1.0.tar.gz.

File metadata

  • Download URL: django_gem-1.1.0.tar.gz
  • Upload date:
  • Size: 16.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.11.0 Linux/5.15.0-1039-azure

File hashes

Hashes for django_gem-1.1.0.tar.gz
Algorithm Hash digest
SHA256 64e21f6f7d6a52f7ab3d10efc60fd954f2f0fd6c2c2fe4fd69b6eddba040ae86
MD5 88b81e35c2fbfa6585c7de1b72c25a94
BLAKE2b-256 ef6109204a4cf4d5c3d2d81d505f6f25567fa201b4725933b4ccb08e5931173c

See more details on using hashes here.

File details

Details for the file django_gem-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: django_gem-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 30.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.11.0 Linux/5.15.0-1039-azure

File hashes

Hashes for django_gem-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6a08c19bee80c94ea51b56aa0e8bf5c8539fe4e002bd047c17a897b5ac62dd39
MD5 928bf9c37d8c956f3e460c893e9df51d
BLAKE2b-256 72e21721c5a03c8769940b007ed9bf0fd1dfedfb222bf7a8a3e0686a8db054aa

See more details on using hashes here.

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