mirror of
https://github.com/Hopiu/django-model-utils.git
synced 2026-03-16 20:00:23 +00:00
chore(monitor-field): Add deprecation warning when the field is null and no default is provided. The new behavior will be introduced in a next major release
This commit is contained in:
parent
6e7158bfa0
commit
22015b8e69
3 changed files with 25 additions and 0 deletions
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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):
|
||||
"""
|
||||
|
|
|
|||
Loading…
Reference in a new issue