Merge pull request #577 from gmcrocetti/monitor-default-none-when-nullable

`MonitorField` - Change default to None when the field is nullable
This commit is contained in:
Jelmer 2024-03-22 14:28:10 +01:00 committed by GitHub
commit fe1d0c6907
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 25 additions and 0 deletions

View file

@ -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

View file

@ -1,5 +1,6 @@
import secrets
import uuid
import warnings
from collections.abc import Callable
from django.conf import settings
@ -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 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)
monitor = kwargs.pop('monitor', None)
if not monitor:

View file

@ -34,6 +34,18 @@ class MonitorFieldTests(TestCase):
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):
"""