Skip to content

Commit 4c9d6ee

Browse files
authored
feat(monitor): Change default to None when the field is marked as nullable and no default is provided (jazzband#599)
1 parent 714632e commit 4c9d6ee

File tree

4 files changed

+14
-23
lines changed

4 files changed

+14
-23
lines changed

CHANGES.rst

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ Changelog
33

44
To be released
55
--------------
6+
- Remove MonitorField deprecation warning. `None` - instead of
7+
`django.utils.timezone.now` will be used when nullable and no default provided (GH-#599)
68
- Add deprecation warning for MonitorField. The default value will be `None`
79
instead of `django.utils.timezone.now` - when nullable and without a default.
810
- Add Brazilian Portuguese translation (GH-#578)

model_utils/fields.py

+2-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import secrets
22
import uuid
3-
import warnings
43

54
from django.conf import settings
65
from django.core.exceptions import ValidationError
@@ -107,17 +106,8 @@ class MonitorField(models.DateTimeField):
107106
"""
108107

109108
def __init__(self, *args, monitor, when=None, **kwargs):
110-
if kwargs.get("null") and kwargs.get("default") is None:
111-
warning_message = (
112-
"{}.default is set to 'django.utils.timezone.now' - when nullable"
113-
" and no default. This behavior will be deprecated in the next"
114-
" major release in favor of 'None'. See"
115-
" https://django-model-utils.readthedocs.io/en/stable/fields.html"
116-
"#monitorfield for more information."
117-
).format(self.__class__.__name__)
118-
warnings.warn(warning_message, DeprecationWarning)
119-
120-
kwargs.setdefault('default', now)
109+
default = None if kwargs.get("null") else now
110+
kwargs.setdefault('default', default)
121111
self.monitor = monitor
122112
if when is not None:
123113
when = set(when)

tests/models.py

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ class TimeFrameManagerAdded(TimeFramedModel):
9898
class Monitored(models.Model):
9999
name = models.CharField(max_length=25)
100100
name_changed = MonitorField(monitor="name")
101+
name_changed_nullable = MonitorField(monitor="name", null=True)
101102

102103

103104
class MonitorWhen(models.Model):

tests/test_fields/test_monitor_field.py

+9-11
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,15 @@ def test_no_monitor_arg(self):
3434
with self.assertRaises(TypeError):
3535
MonitorField()
3636

37-
def test_nullable_without_default_deprecation(self):
38-
warning_message = (
39-
"{}.default is set to 'django.utils.timezone.now' - when nullable"
40-
" and no default. This behavior will be deprecated in the next"
41-
" major release in favor of 'None'. See"
42-
" https://django-model-utils.readthedocs.io/en/stable/fields.html"
43-
"#monitorfield for more information."
44-
).format(MonitorField.__name__)
45-
46-
with self.assertWarns(DeprecationWarning, msg=warning_message):
47-
MonitorField(monitor="foo", null=True, default=None)
37+
def test_monitor_default_is_none_when_nullable(self):
38+
self.assertIsNone(self.instance.name_changed_nullable)
39+
expected_datetime = datetime(2022, 1, 18, 12, 0, 0, tzinfo=timezone.utc)
40+
41+
self.instance.name = "Jose"
42+
with time_machine.travel(expected_datetime, tick=False):
43+
self.instance.save()
44+
45+
self.assertEqual(self.instance.name_changed_nullable, expected_datetime)
4846

4947

5048
class MonitorWhenFieldTests(TestCase):

0 commit comments

Comments
 (0)