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.0.tar.gz (19.6 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.0-py3-none-any.whl (22.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_model_versioning-0.1.0.tar.gz
  • Upload date:
  • Size: 19.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.3 Darwin/23.4.0

File hashes

Hashes for django_model_versioning-0.1.0.tar.gz
Algorithm Hash digest
SHA256 75886573d180a23ba4add230ac715e1d92ef94c039bbf14474f94defddef4bdd
MD5 5236d7b247afe08f327928891444c57d
BLAKE2b-256 27593cc2029b1f5ebc6a1dd498a772668c4fa133514625856f70399e6012cfe8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_model_versioning-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 583ad1c407d7f79109ea25f40ab92a459f71596c171496b2f7e6cacfcce1fad1
MD5 72fd3bde1828b094666c73ef1c0d2f15
BLAKE2b-256 d2d36ee062e69093c757524ce213ace3e9c82455c2be0373ff5ad622bede40a1

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