From 7303cd7b4941b778736a3a6dc4918a2defaee278 Mon Sep 17 00:00:00 2001 From: Nestor Arocha Date: Fri, 18 Mar 2022 15:08:15 +0000 Subject: [PATCH 1/3] fix https://github.com/jazzband/django-model-utils/issues/520 --- model_utils/fields.py | 11 ++++++++--- tests/test_models/test_timestamped_model.py | 9 +++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/model_utils/fields.py b/model_utils/fields.py index ce05e9f3..b7718664 100644 --- a/model_utils/fields.py +++ b/model_utils/fields.py @@ -5,6 +5,7 @@ from django.conf import settings from django.core.exceptions import ValidationError from django.db import models +from django.utils.functional import cached_property from django.utils.timezone import now DEFAULT_CHOICES_NAME = 'STATUS' @@ -34,9 +35,13 @@ class AutoLastModifiedField(AutoCreatedField): """ def get_default(self): """Return the default value for this field.""" - if not hasattr(self, "_default"): - self._default = self._get_default() - return self._default + return self._get_default() + + @cached_property + def _get_default(self): + if callable(self.default): + return self.default + return lambda: self.default def pre_save(self, model_instance, add): value = now() diff --git a/tests/test_models/test_timestamped_model.py b/tests/test_models/test_timestamped_model.py index 0d139141..bb63d01e 100644 --- a/tests/test_models/test_timestamped_model.py +++ b/tests/test_models/test_timestamped_model.py @@ -28,6 +28,15 @@ def test_modified(self): self.assertEqual(t1.modified, datetime(2016, 1, 2)) + def test_modified_is_not_cached(self): + with freeze_time(datetime(2016, 1, 1)): + t1 = TimeStamp.objects.create() + + with freeze_time(datetime(2017, 1, 1)): + t2 = TimeStamp.objects.create() + + self.assertNotEqual(t1.modified, t2.modified) + def test_overriding_created_via_object_creation_also_uses_creation_date_for_modified(self): """ Setting the created date when first creating an object From ec290adea3869b770ee4bf9db56f1235a158b49d Mon Sep 17 00:00:00 2001 From: Nestor Arocha Date: Fri, 18 Mar 2022 15:10:56 +0000 Subject: [PATCH 2/3] update CHANGES.rst --- CHANGES.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.rst b/CHANGES.rst index 320fdea5..781c0a4c 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -7,6 +7,7 @@ Unreleased - Confirm support for `Django 4.0` - Add Spanish translation - Add French translation +- Fix AutoLastModifiedField cache (GH-#520) 4.2.0 (2021-10-11) ------------------ From 9812148a2fcb37c02b60e99ea795abc2e1230bc9 Mon Sep 17 00:00:00 2001 From: Nestor Arocha Date: Fri, 18 Mar 2022 15:53:12 +0000 Subject: [PATCH 3/3] make test fail --- tests/test_models/test_timestamped_model.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_models/test_timestamped_model.py b/tests/test_models/test_timestamped_model.py index bb63d01e..f8f5f4c0 100644 --- a/tests/test_models/test_timestamped_model.py +++ b/tests/test_models/test_timestamped_model.py @@ -30,10 +30,10 @@ def test_modified(self): def test_modified_is_not_cached(self): with freeze_time(datetime(2016, 1, 1)): - t1 = TimeStamp.objects.create() + t1 = TimeStamp() with freeze_time(datetime(2017, 1, 1)): - t2 = TimeStamp.objects.create() + t2 = TimeStamp() self.assertNotEqual(t1.modified, t2.modified)