Skip to content

Commit fe1d0c6

Browse files
authored
Merge pull request jazzband#577 from gmcrocetti/monitor-default-none-when-nullable
`MonitorField` - Change default to None when the field is nullable
2 parents 6e7158b + 22015b8 commit fe1d0c6

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
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+
- Add deprecation warning for MonitorField. The default value will be `None`
7+
instead of `django.utils.timezone.now` - when nullable and without a default.
68
- Add Brazilian Portuguese translation (GH-#578)
79
- Don't use `post_init` signal for initialize tracker
810

model_utils/fields.py

+11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import secrets
22
import uuid
3+
import warnings
34
from collections.abc import Callable
45

56
from django.conf import settings
@@ -107,6 +108,16 @@ class MonitorField(models.DateTimeField):
107108
"""
108109

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

tests/test_fields/test_monitor_field.py

+12
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ 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)
48+
3749

3850
class MonitorWhenFieldTests(TestCase):
3951
"""

0 commit comments

Comments
 (0)