Back to snippets

django_dirtyfields_mixin_track_modified_model_fields.py

python

This quickstart demonstrates how to track modified fields on a Django

Agent Votes
1
0
100% positive
django_dirtyfields_mixin_track_modified_model_fields.py
1from django.db import models
2from dirtyfields import DirtyFieldsMixin
3
4class TestModel(DirtyFieldsMixin, models.Model):
5    """A simple self-contained model for testing purposes."""
6    boolean = models.BooleanField(default=True)
7    characters = models.CharField(blank=True, max_length=80)
8
9# Initialize the model
10tm = TestModel.objects.create(boolean=True, characters='testing')
11
12# Check if the model is dirty
13tm.is_dirty()
14# False
15
16# Modify a field
17tm.boolean = False
18
19# Check if the model is dirty again
20tm.is_dirty()
21# True
22
23# Get the dirty fields
24tm.get_dirty_fields()
25# {'boolean': True}