Opened 7 years ago
Closed 7 years ago
#28673 closed Uncategorized (invalid)
Make null=True explicitly uncombinable with auto_now=True
Reported by: | Дилян Палаузов | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.11 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -1147,6 +1147,13 @@ class DateTimeCheckMixin(object): # auto_now, auto_now_add, and default are mutually exclusive # options. The use of more than one of these options together # will trigger an Error + if self.auto_now and self.null: + return [ + checks.Error( + "The option null=True cannot be used with auto_now.", + obj=self, + id='fields.E161', + )] mutually_exclusive_options = [self.auto_now_add, self.auto_now, self.has_default()] enabled_options = [option not in (None, False) for option in mutually_exclusive_options].count(True) if enabled_options > 1: diff --git a/docs/ref/checks.txt b/docs/ref/checks.txt --- a/docs/ref/checks.txt +++ b/docs/ref/checks.txt @@ -167,6 +167,7 @@ Model fields null values are not allowed, as blank values are stored as nulls. * **fields.E160**: The options ``auto_now``, ``auto_now_add``, and ``default`` are mutually exclusive. Only one of these options may be present. +* **fields.E161**: ``null=True`` cannot be used with ``auto_now``. * **fields.W161**: Fixed default value provided. * **fields.E900**: ``IPAddressField`` has been removed except for support in historical migrations. diff --git a/docs/ref/models/fields.txt b/docs/ref/models/fields.txt --- a/docs/ref/models/fields.txt +++ b/docs/ref/models/fields.txt @@ -503,7 +503,8 @@ optional arguments: Automatically set the field to now every time the object is saved. Useful for "last-modified" timestamps. Note that the current date is *always* - used; it's not just a default value that you can override. + used; it's not just a default value that you can override, hence this cannot + be combined with ``null=True``. The field is only automatically updated when calling :meth:`Model.save() <django.db.models.Model.save>`. The field isn't updated when making updates
Change History (3)
comment:1 by , 7 years ago
comment:2 by , 7 years ago
From within Django the value of an auto_now=True field cannot be stored as None, and when the value is set to None outside of Django, it is irrelevant whether null=True of the field description is set in Django.
Within Django allowing simultaneously auto_now = null = True can only lead to confusion and I see no added value.
comment:3 by , 7 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
With an ...objects.update() it can be set to None.
Note:
See TracTickets
for help on using tickets.
I don't see why the two options couldn't be used together. Consider the use case of adding a column to an existing table -- existing rows might be null.