Skip to content

Commit

Permalink
Add a way to save only dirty fields by default
Browse files Browse the repository at this point in the history
We create an extra class that can be used when the user wants to always
save only dirty fields when calling save().
  • Loading branch information
Timvde authored and nielsvanoch committed Jul 11, 2024
1 parent 71945d3 commit 7aebf22
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/dirtyfields/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

__all__ = ['DirtyFieldsMixin']
__version__ = "1.9.4.dev0"
from dirtyfields.dirtyfields import DirtyFieldsMixin

from dirtyfields.dirtyfields import DefaultDirtyFieldsMixin, DirtyFieldsMixin

VERSION = tuple(map(int, __version__.split(".")[0:3]))
16 changes: 14 additions & 2 deletions src/dirtyfields/dirtyfields.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,22 @@ def is_dirty(self, check_relationship=False, check_m2m=None):

def save_dirty_fields(self):
if self._state.adding:
self.save()
super().save()
else:
dirty_fields = self.get_dirty_fields(check_relationship=True)
self.save(update_fields=dirty_fields.keys())
super().save(update_fields=dirty_fields.keys())


class DefaultDirtyFieldsMixin(DirtyFieldsMixin):
"""
Save dirty fields automatically when calling save()
"""

def save(self, *args, save_all=False, **kwargs):
if save_all:
super().save(*args, **kwargs)
else:
self.save_dirty_fields()


def reset_state(sender, instance, **kwargs):
Expand Down

0 comments on commit 7aebf22

Please sign in to comment.