Skip to main content

A library to version a specific model, with references to specific and/or last version of it

Project description

Django Model Versioning

This library is used to create a model that get automatically versioned when specific fields are updated. When creating a relationship to such amodel you can choose if you want to create a standard relationship, that will connect your record to that specific version of the model, or create a relationship that will always point to the most updated version of the versioned entity instead.

Usage

  1. First create the model you'd like to version like you'd normally do.
# myproject/taxes/models.py
from django.db.models import Model, CharField, DateTimeField, DecimalField


class Tax(Model):
    name = CharField(max_length=255, blank=False, null=False)
    percentage_value = DecimalField(max_digits=10, decimal_places=2, default=0)

    created_at = DateTimeField(auto_now_add=True)
    updated_at = DateTimeField(auto_now=True)
  1. Then within your app create a versioning.py file
from lib.register import register
from lib.versioning_options import VersioningOptions
from taxes.models import Tax


@register(Tax)
class TaxVersioningOptions(VersioningOptions):
    versioned_fields = ('percentage_value',)
  • We user @register(Tax) to tell django that these versioning options refer to the Tax model.
  • The name of the class is unimportant, but it has to inherit from VersioningOptions
  • Then we create the versioned_fields property and we specify which fields from the Tax model (or whatever model you register these options for) should be versioned. Whenever a versioned_field gets updated, a new version of the model is created and all references get updated.
  1. Now we create a model that refers to that specific instance. We can just use a standard ForeignKey since the nehaviour we need here is the stadard one for Django references
from django.db.models import Model, IntegerField, DO_NOTHING, DateTimeField, ForeignKey

from taxes.models import Tax


class Payment(Model):
    amount = IntegerField()

    tax = ForeignKey(Tax, on_delete=DO_NOTHING)

    created_at = DateTimeField(auto_now_add=True)
    updated_at = DateTimeField(auto_now=True)

So in this case, each Payment we created will always refer to the same version of Tax 4. Now we create another model. This time we ant our new model to always refer to the last version of Tax

from django.db.models import Model, CharField, IntegerField, DateTimeField, DO_NOTHING

from lib.versioned_foreign_key import VersionedForeignKey
from taxes.models import Tax


class Product(Model):
    name = CharField(max_length=255, null=False, blank=False)
    price = IntegerField(null=False, default=0)

    tax = VersionedForeignKey(Tax, on_delete=DO_NOTHING)

    created_at = DateTimeField(auto_now_add=True)
    updated_at = DateTimeField(auto_now=True)

As we can see in the example, this time we don't use a ForeignField but rather a VersionedForeignField

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_model_versioning-0.1.1.tar.gz (19.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_model_versioning-0.1.1-py3-none-any.whl (22.0 kB view details)

Uploaded Python 3

File details

Details for the file django_model_versioning-0.1.1.tar.gz.

File metadata

  • Download URL: django_model_versioning-0.1.1.tar.gz
  • Upload date:
  • Size: 19.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.5 Darwin/23.5.0

File hashes

Hashes for django_model_versioning-0.1.1.tar.gz
Algorithm Hash digest
SHA256 86a366dba1ee3441d9a2d27ee30dc52a94dc81d0a53d79cbce1a2d685244c7cd
MD5 00106cf0648eb030702e060ec72b7c78
BLAKE2b-256 b507a0e73a6f5e901382e1e5caeb670d1508014d6d04533c1afe1c1bff4108ec

See more details on using hashes here.

File details

Details for the file django_model_versioning-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_model_versioning-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f2e8b4dbc272ecfc5ecc67e1a176bf8343adad2e71dbc9419c4ee1c742f8a303
MD5 0b543650b8525934d5b235aaf3bbe11a
BLAKE2b-256 87abba5e26b6a5126deec1ad415fc0262b8a226b9db3017a1b9424102508de52

See more details on using hashes here.

Supported by

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