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.
Metadata
Release files for django-model-snapshot-diff 1.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django-model-snapshot-diff-1.0.1.tar.gz | 4.3 kB | Details |
Release files / django-model-snapshot-diff-1.0.1.tar.gz
| Download URL | django-model-snapshot-diff-1.0.1.tar.gz |
|---|---|
| Size | 4.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
839912f494b364b9db0e9c48f66afc1823ff4fc99b757a60bbe2661f7fc4da94
|
|
BLAKE2b-256 checksum How to use checksums |
cd3c3fa0d24f623cb4e7d0c144ffae4b31e0b3468a153c63b8c958c849181cb6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|