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 committed Sep 2, 2022
1 parent 4398310 commit be35025
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/dirtyfields/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
__all__ = ['DirtyFieldsMixin']
__version__ = "1.8.3.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 be35025

Please sign in to comment.