https://github.com/apkawa/django-snapshot-field
Snapshot model to field
https://github.com/apkawa/django-snapshot-field
Last synced: 6 months ago
JSON representation
Snapshot model to field
- Host: GitHub
- URL: https://github.com/apkawa/django-snapshot-field
- Owner: Apkawa
- License: mit
- Created: 2018-03-22T13:30:09.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2025-01-11T06:49:14.000Z (over 1 year ago)
- Last Synced: 2025-04-16T22:05:05.721Z (over 1 year ago)
- Language: Python
- Size: 61.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/Apkawa/django-snapshot-field/actions/workflows/ci.yml)
[](https://pypi.python.org/pypi/django-snapshot-field)
[](https://pypi.python.org/pypi/django-snapshot-field)
[](LICENSE)
A field in a model that stores a snapshot of a model object and retrieves it as a read-only model object
# Installation
```bash
pip install django-snapshot-field
```
or from git
```bash
pip install -e git+https://githib.com/Apkawa/django-snapshot-field.git#egg=django-snapshot-field
```
## Django and python version compatibles
| Python
Django | 3.8 | 3.9 | 3.10 | 3.11 | 3.12 | 3.13 |
|:-----------------:|-----|----|------|------|------|------|
| 4.2 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| 5.0 | ❌ | ❌ | ✅ | ✅ | ✅ | ✅ |
| 5.1 | ❌ | ❌ | ✅ | ✅ | ✅ | ✅ |
| 5.2 | ❌ | ❌ | ✅ | ✅ | ✅ | ✅ |
# Usage
```python
from django.db import models
from snapshot_field.fields import SnapshotModelField
class Example(models.Model):
name = models.CharField(max_length=20)
class ExampleReference(models.Model):
name = models.CharField(max_length=20)
ref = models.ForeignKey(Example)
class ExampleSnapshotModel(models.Model):
snapshot = SnapshotModelField(null=True)
snapshot_refs = SnapshotModelField(
['tests.Example', ['ExampleReference', {'fields': ['name', 'ref'], 'refs': ['ref']}]]
)
obj = Example.objects.create(name='test_name')
obj_ref = ExampleReference.objects.create(name='refname', ref=obj)
snap = ExampleSnapshotModel.objects.create(snapshot=obj, snapshot_refs=obj_ref)
assert snap.snapshot.name == obj.name
assert snap.snapshot_refs.name == obj_ref.name
assert snap.snapshot_refs.ref.name == obj.name
obj.delete()
obj_ref.delete()
snap.refresh_from_db()
assert snap.snapshot.name == obj.name
assert snap.snapshot_refs.name == obj_ref.name
assert snap.snapshot_refs.ref.name == obj.name
```