mirror of
https://github.com/django/django.git
synced 2025-07-24 13:44:32 +00:00
Fixed #21798 -- Added check for DateTime mutually exclusive options
Added DateTimeCheckMixin to avoid the use of default, auto_now, and auto_now_add options together. Added the fields.E151 Error that is raised if one or more of these options are used together.
This commit is contained in:
parent
8a9d54aa69
commit
cb15231888
4 changed files with 64 additions and 2 deletions
|
@ -1,6 +1,7 @@
|
|||
# -*- encoding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from datetime import datetime
|
||||
import unittest
|
||||
|
||||
from django.core.checks import Error
|
||||
|
@ -399,3 +400,30 @@ class ImageFieldTests(IsolatedModelsTestCase):
|
|||
),
|
||||
]
|
||||
self.assertEqual(errors, expected)
|
||||
|
||||
|
||||
class DateFieldTests(IsolatedModelsTestCase):
|
||||
|
||||
def test_auto_now_and_auto_now_add_raise_error(self):
|
||||
dn = datetime.now
|
||||
mutually_exclusive_combinations = (
|
||||
(True, True, dn),
|
||||
(True, False, dn),
|
||||
(False, True, dn),
|
||||
(True, True, None)
|
||||
)
|
||||
|
||||
for auto_now, auto_now_add, default in mutually_exclusive_combinations:
|
||||
field = models.DateTimeField(name="field", auto_now=auto_now,
|
||||
auto_now_add=auto_now_add,
|
||||
default=default)
|
||||
expected = [Error(
|
||||
"The options auto_now, auto_now_add, and default "
|
||||
"are mutually exclusive. Only one of these options "
|
||||
"may be present.",
|
||||
hint=None,
|
||||
obj=field,
|
||||
id='fields.E151',
|
||||
)]
|
||||
checks = field.check()
|
||||
self.assertEqual(checks, expected)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue