File tree 3 files changed +25
-0
lines changed
3 files changed +25
-0
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,8 @@ Changelog
3
3
4
4
To be released
5
5
--------------
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.
6
8
- Add Brazilian Portuguese translation (GH-#578)
7
9
- Don't use `post_init ` signal for initialize tracker
8
10
Original file line number Diff line number Diff line change 1
1
import secrets
2
2
import uuid
3
+ import warnings
3
4
from collections .abc import Callable
4
5
5
6
from django .conf import settings
@@ -107,6 +108,16 @@ class MonitorField(models.DateTimeField):
107
108
"""
108
109
109
110
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
+
110
121
kwargs .setdefault ('default' , now )
111
122
monitor = kwargs .pop ('monitor' , None )
112
123
if not monitor :
Original file line number Diff line number Diff line change @@ -34,6 +34,18 @@ def test_no_monitor_arg(self):
34
34
with self .assertRaises (TypeError ):
35
35
MonitorField ()
36
36
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
+
37
49
38
50
class MonitorWhenFieldTests (TestCase ):
39
51
"""
You can’t perform that action at this time.
0 commit comments