Skip to content

Commit

Permalink
feat(monitor): Change default to None when the field is marked as nul…
Browse files Browse the repository at this point in the history
…lable
  • Loading branch information
gmcrocetti committed Mar 21, 2024
1 parent 6e7158b commit a3401db
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Changelog

To be released
--------------
- 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)
- Don't use `post_init` signal for initialize tracker

Expand Down
11 changes: 11 additions & 0 deletions model_utils/fields.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import secrets
import uuid
import warnings
from collections.abc import Callable

from django.conf import settings
Expand Down Expand Up @@ -107,6 +108,16 @@ 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 without 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)
monitor = kwargs.pop('monitor', None)
if not monitor:
Expand Down
1 change: 1 addition & 0 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
12 changes: 12 additions & 0 deletions tests/test_fields/test_monitor_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ 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)


class MonitorWhenFieldTests(TestCase):
"""
Expand Down

0 comments on commit a3401db

Please sign in to comment.