Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MonitorField - Change default to None when the field is nullable #599

Merged
merged 2 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading