#28673: Make null=True explicitly uncombinable with auto_now=True
-------------------------------------+-------------------------------------
               Reporter:  Дилян      |          Owner:  nobody
  Палаузов                           |
                   Type:             |         Status:  new
  Uncategorized                      |
              Component:  Database   |        Version:  1.11
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 {{{
 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
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/28673>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/057.00ce614be15205181fcbc9a0c568dfe7%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to