From be35025b9f02c0155569fb5f6b9a8d13ed061156 Mon Sep 17 00:00:00 2001 From: Tim Van den Eynde Date: Fri, 2 Sep 2022 14:59:05 +0200 Subject: [PATCH] Add a way to save only dirty fields by default We create an extra class that can be used when the user wants to always save only dirty fields when calling save(). --- src/dirtyfields/__init__.py | 2 +- src/dirtyfields/dirtyfields.py | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/dirtyfields/__init__.py b/src/dirtyfields/__init__.py index ab41041..3b2cbce 100644 --- a/src/dirtyfields/__init__.py +++ b/src/dirtyfields/__init__.py @@ -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])) diff --git a/src/dirtyfields/dirtyfields.py b/src/dirtyfields/dirtyfields.py index 740f6a0..650d13c 100644 --- a/src/dirtyfields/dirtyfields.py +++ b/src/dirtyfields/dirtyfields.py @@ -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):