Skip to main content

Django model snapshot difference

Project description

Django model snapshot difference

Make snapshot from Django model with all deep relations and get difference

Example models with other relations like OneToOne, ManyToMany etc.:

from django.db import models
from django_model_snapshot_diff.mixins import SnapshotMixin
from django_model_snapshot_diff.models import SnapshotModel


class ManyToMany(SnapshotMixin, models.Model):
    description = models.CharField(max_length=255)

    snapshot_fields = [
        ('description', 'ManyToMany DESCRIPTION field'),
    ]


class ForeignKeyDeeper(SnapshotMixin, models.Model):
    name = models.CharField(max_length=255)
    many_to_many = models.ManyToManyField('example.ManyToMany')

    snapshot_fields = [
        ('name', 'ForeignKeyDeeper NAME field'),
        ('many_to_many', 'ForeignKeyDeeper MANYTOMANY field'),
    ]


class ForeignKey(SnapshotMixin, models.Model):
    title = models.CharField(max_length=255)
    foreign_key_deeper = models.ForeignKey('example.ForeignKeyDeeper',
                                           on_delete=models.CASCADE)

    snapshot_fields = [
        ('title', 'ForeignKey TITLE field'),
        ('foreign_key_deeper', 'ForeignKey DEEPER field'),
    ]


class Relation(SnapshotMixin, models.Model):
    choice = models.CharField(choices=((1, 'A'), (2, 'B')), default=1,
                              max_length=2)
    rel_to_example = models.ForeignKey('example.Example',
                                       on_delete=models.CASCADE,
                                       related_name='relation_to_example')

    snapshot_fields = [
        ('choice', 'Relation CHOICE field'),
    ]


class Example(SnapshotModel):
    text = models.CharField(max_length=255)
    boolean = models.BooleanField(default=False)
    datetime = models.DateTimeField(auto_now=True)
    foreign_key = models.ForeignKey('example.ForeignKey',
                                    on_delete=models.CASCADE)

    snapshot_fields = [
        ('text', 'Example TEXT field'),
        ('boolean', 'Example BOOL field'),
        ('datetime', 'Example DATETIME field'),
        ('foreign_key', 'Example ForeignKey field'),
        ('relation_to_example', 'Example Relation field'),
    ]

snapshot_fields should be a list of tuples with fields to track, for example:

snapshot_fields = [
    ('model_field_name', 'Verbose name, any text')
]

How to use?

example = Example.objects.first()
example.save_snapshot()

example.text = 'new text'
example.save()

diff = example.diff(verbose=True)
assert diff == {'Example TEXT field': {'New Value:': 'new text',
                                        'Old Value': 'text'}}

Diff will contain all difference with relations, for full example look for a tests.

Testing

Tests are found in a simplified Django project in the /testapp folder. Install the project requirements and do ./manage.py test to run them.

License

See License.

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-snapshot-diff-1.0.1.tar.gz (4.3 kB view details)

Uploaded Source

File details

Details for the file django-model-snapshot-diff-1.0.1.tar.gz.

File metadata

  • Download URL: django-model-snapshot-diff-1.0.1.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.7.7

File hashes

Hashes for django-model-snapshot-diff-1.0.1.tar.gz
Algorithm Hash digest
SHA256 839912f494b364b9db0e9c48f66afc1823ff4fc99b757a60bbe2661f7fc4da94
MD5 62aa9dcfe3155f279722c0dd1426613f
BLAKE2b-256 cd3c3fa0d24f623cb4e7d0c144ffae4b31e0b3468a153c63b8c958c849181cb6

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