From 5eab41f6177283fd377e33f3f411baaf2f406580 Mon Sep 17 00:00:00 2001 From: Guilherme Martins Crocetti <24530683+gmcrocetti@users.noreply.github.com> Date: Thu, 21 Mar 2024 20:05:05 -0300 Subject: [PATCH] feat(monitor): Change default to None when the field is marked as nullable and no default is provided --- CHANGES.rst | 2 ++ model_utils/fields.py | 14 ++------------ tests/models.py | 1 + tests/test_fields/test_monitor_field.py | 20 +++++++++----------- 4 files changed, 14 insertions(+), 23 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index e5e48e59..3cfeccc2 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -3,6 +3,8 @@ Changelog To be released -------------- +- Remove MonitorField deprecation warning. `None` - instead of + `django.utils.timezone.now` will be used when nullable and no default provided (GH-#599) - Add deprecation warning for MonitorField. The default value will be `None` instead of `django.utils.timezone.now` - when nullable and without a default. - Add Brazilian Portuguese translation (GH-#578) diff --git a/model_utils/fields.py b/model_utils/fields.py index e04ada2b..2fdd0b5e 100644 --- a/model_utils/fields.py +++ b/model_utils/fields.py @@ -1,6 +1,5 @@ import secrets import uuid -import warnings from collections.abc import Callable from django.conf import settings @@ -108,17 +107,8 @@ class MonitorField(models.DateTimeField): """ def __init__(self, *args, **kwargs): - if kwargs.get("null") and kwargs.get("default") is None: - warning_message = ( - "{}.default is set to 'django.utils.timezone.now' - when nullable" - " and no default. This behavior will be deprecated in the next" - " major release in favor of 'None'. See" - " https://django-model-utils.readthedocs.io/en/stable/fields.html" - "#monitorfield for more information." - ).format(self.__class__.__name__) - warnings.warn(warning_message, DeprecationWarning) - - kwargs.setdefault('default', now) + default = None if kwargs.get("null") else now + kwargs.setdefault('default', default) monitor = kwargs.pop('monitor', None) if not monitor: raise TypeError( diff --git a/tests/models.py b/tests/models.py index 6a5849e6..158bab7b 100644 --- a/tests/models.py +++ b/tests/models.py @@ -98,6 +98,7 @@ class TimeFrameManagerAdded(TimeFramedModel): class Monitored(models.Model): name = models.CharField(max_length=25) name_changed = MonitorField(monitor="name") + name_changed_nullable = MonitorField(monitor="name", null=True) class MonitorWhen(models.Model): diff --git a/tests/test_fields/test_monitor_field.py b/tests/test_fields/test_monitor_field.py index 8c53571d..f0041368 100644 --- a/tests/test_fields/test_monitor_field.py +++ b/tests/test_fields/test_monitor_field.py @@ -34,17 +34,15 @@ def test_no_monitor_arg(self): with self.assertRaises(TypeError): MonitorField() - def test_nullable_without_default_deprecation(self): - warning_message = ( - "{}.default is set to 'django.utils.timezone.now' - when nullable" - " and no default. This behavior will be deprecated in the next" - " major release in favor of 'None'. See" - " https://django-model-utils.readthedocs.io/en/stable/fields.html" - "#monitorfield for more information." - ).format(MonitorField.__name__) - - with self.assertWarns(DeprecationWarning, msg=warning_message): - MonitorField(monitor="foo", null=True, default=None) + def test_monitor_default_is_none_when_nullable(self): + self.assertIsNone(self.instance.name_changed_nullable) + expected_datetime = datetime(2022, 1, 18, 12, 0, 0, tzinfo=timezone.utc) + + self.instance.name = "Jose" + with time_machine.travel(expected_datetime, tick=False): + self.instance.save() + + self.assertEqual(self.instance.name_changed_nullable, expected_datetime) class MonitorWhenFieldTests(TestCase):