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 and no default is provided (#599)
  • Loading branch information
gmcrocetti authored Apr 4, 2024
1 parent 714632e commit 4c9d6ee
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 23 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
--------------
- 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)
Expand Down
14 changes: 2 additions & 12 deletions model_utils/fields.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import secrets
import uuid
import warnings

from django.conf import settings
from django.core.exceptions import ValidationError
Expand Down Expand Up @@ -107,17 +106,8 @@ class MonitorField(models.DateTimeField):
"""

def __init__(self, *args, monitor, when=None, **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)
self.monitor = monitor
if when is not None:
when = set(when)
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
20 changes: 9 additions & 11 deletions tests/test_fields/test_monitor_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 4c9d6ee

Please sign in to comment.