Fixed #23765 -- Removed BooleanField default check which often yielded false positives.
This commit is contained in:
parent
d5a109f6e6
commit
c24a2e6cbd
|
@ -7,7 +7,6 @@ from .messages import (CheckMessage,
|
||||||
from .registry import register, run_checks, tag_exists, Tags
|
from .registry import register, run_checks, tag_exists, Tags
|
||||||
|
|
||||||
# Import these to force registration of checks
|
# Import these to force registration of checks
|
||||||
import django.core.checks.compatibility.django_1_6_0 # NOQA
|
|
||||||
import django.core.checks.compatibility.django_1_7_0 # NOQA
|
import django.core.checks.compatibility.django_1_7_0 # NOQA
|
||||||
import django.core.checks.model_checks # NOQA
|
import django.core.checks.model_checks # NOQA
|
||||||
import django.core.checks.security.base # NOQA
|
import django.core.checks.security.base # NOQA
|
||||||
|
|
|
@ -1,41 +0,0 @@
|
||||||
# -*- encoding: utf-8 -*-
|
|
||||||
from __future__ import unicode_literals
|
|
||||||
|
|
||||||
from django.apps import apps
|
|
||||||
|
|
||||||
from .. import Warning, register, Tags
|
|
||||||
|
|
||||||
|
|
||||||
@register(Tags.compatibility)
|
|
||||||
def check_1_6_compatibility(**kwargs):
|
|
||||||
errors = []
|
|
||||||
errors.extend(_check_boolean_field_default_value(**kwargs))
|
|
||||||
return errors
|
|
||||||
|
|
||||||
|
|
||||||
def _check_boolean_field_default_value(app_configs=None, **kwargs):
|
|
||||||
"""
|
|
||||||
Checks if there are any BooleanFields without a default value, &
|
|
||||||
warns the user that the default has changed from False to None.
|
|
||||||
"""
|
|
||||||
from django.db import models
|
|
||||||
|
|
||||||
problem_fields = [
|
|
||||||
field
|
|
||||||
for model in apps.get_models(**kwargs)
|
|
||||||
if app_configs is None or model._meta.app_config in app_configs
|
|
||||||
for field in model._meta.local_fields
|
|
||||||
if isinstance(field, models.BooleanField) and not field.has_default()
|
|
||||||
]
|
|
||||||
|
|
||||||
return [
|
|
||||||
Warning(
|
|
||||||
"BooleanField does not have a default value.",
|
|
||||||
hint=("Django 1.6 changed the default value of BooleanField from False to None. "
|
|
||||||
"See https://docs.djangoproject.com/en/1.6/ref/models/fields/#booleanfield "
|
|
||||||
"for more information."),
|
|
||||||
obj=field,
|
|
||||||
id='1_6.W002',
|
|
||||||
)
|
|
||||||
for field in problem_fields
|
|
||||||
]
|
|
|
@ -168,7 +168,8 @@ that might occur as a result of a version upgrade.
|
||||||
|
|
||||||
* **1_6.W001**: Some project unit tests may not execute as expected. *This
|
* **1_6.W001**: Some project unit tests may not execute as expected. *This
|
||||||
check was removed in Django 1.8 due to false positives*.
|
check was removed in Django 1.8 due to false positives*.
|
||||||
* **1_6.W002**: ``BooleanField`` does not have a default value.
|
* **1_6.W002**: ``BooleanField`` does not have a default value. *This
|
||||||
|
check was removed in Django 1.8 due to false positives*.
|
||||||
* **1_7.W001**: Django 1.7 changed the global defaults for the
|
* **1_7.W001**: Django 1.7 changed the global defaults for the
|
||||||
``MIDDLEWARE_CLASSES.``
|
``MIDDLEWARE_CLASSES.``
|
||||||
``django.contrib.sessions.middleware.SessionMiddleware``,
|
``django.contrib.sessions.middleware.SessionMiddleware``,
|
||||||
|
|
|
@ -7,8 +7,3 @@ from django.db import models
|
||||||
class SimpleModel(models.Model):
|
class SimpleModel(models.Model):
|
||||||
field = models.IntegerField()
|
field = models.IntegerField()
|
||||||
manager = models.manager.Manager()
|
manager = models.manager.Manager()
|
||||||
|
|
||||||
|
|
||||||
class Book(models.Model):
|
|
||||||
title = models.CharField(max_length=250)
|
|
||||||
is_published = models.BooleanField(default=False)
|
|
||||||
|
|
|
@ -10,17 +10,15 @@ from django.core import checks
|
||||||
from django.core.checks import Error, Warning
|
from django.core.checks import Error, Warning
|
||||||
from django.core.checks.model_checks import check_all_models
|
from django.core.checks.model_checks import check_all_models
|
||||||
from django.core.checks.registry import CheckRegistry
|
from django.core.checks.registry import CheckRegistry
|
||||||
from django.core.checks.compatibility.django_1_6_0 import check_1_6_compatibility
|
|
||||||
from django.core.checks.compatibility.django_1_7_0 import check_1_7_compatibility
|
from django.core.checks.compatibility.django_1_7_0 import check_1_7_compatibility
|
||||||
from django.core.management.base import CommandError
|
from django.core.management.base import CommandError
|
||||||
from django.core.management import call_command
|
from django.core.management import call_command
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.db.models.fields import NOT_PROVIDED
|
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from django.test.utils import override_settings, override_system_checks
|
from django.test.utils import override_settings, override_system_checks
|
||||||
from django.utils.encoding import force_text
|
from django.utils.encoding import force_text
|
||||||
|
|
||||||
from .models import SimpleModel, Book
|
from .models import SimpleModel
|
||||||
|
|
||||||
|
|
||||||
class DummyObj(object):
|
class DummyObj(object):
|
||||||
|
@ -114,32 +112,6 @@ class MessageTests(TestCase):
|
||||||
self.assertEqual(force_text(e), expected)
|
self.assertEqual(force_text(e), expected)
|
||||||
|
|
||||||
|
|
||||||
class Django_1_6_0_CompatibilityChecks(TestCase):
|
|
||||||
|
|
||||||
@override_settings(TEST_RUNNER='myapp.test.CustomRunner')
|
|
||||||
def test_boolean_field_default_value(self):
|
|
||||||
# We patch the field's default value to trigger the warning
|
|
||||||
boolean_field = Book._meta.get_field('is_published')
|
|
||||||
old_default = boolean_field.default
|
|
||||||
try:
|
|
||||||
boolean_field.default = NOT_PROVIDED
|
|
||||||
errors = check_1_6_compatibility()
|
|
||||||
expected = [
|
|
||||||
checks.Warning(
|
|
||||||
'BooleanField does not have a default value.',
|
|
||||||
hint=('Django 1.6 changed the default value of BooleanField from False to None. '
|
|
||||||
'See https://docs.djangoproject.com/en/1.6/ref/models/fields/#booleanfield '
|
|
||||||
'for more information.'),
|
|
||||||
obj=boolean_field,
|
|
||||||
id='1_6.W002',
|
|
||||||
)
|
|
||||||
]
|
|
||||||
self.assertEqual(errors, expected)
|
|
||||||
finally:
|
|
||||||
# Restore the ``default``
|
|
||||||
boolean_field.default = old_default
|
|
||||||
|
|
||||||
|
|
||||||
class Django_1_7_0_CompatibilityChecks(TestCase):
|
class Django_1_7_0_CompatibilityChecks(TestCase):
|
||||||
|
|
||||||
@override_settings(MIDDLEWARE_CLASSES=('django.contrib.sessions.middleware.SessionMiddleware',))
|
@override_settings(MIDDLEWARE_CLASSES=('django.contrib.sessions.middleware.SessionMiddleware',))
|
||||||
|
|
Loading…
Reference in New Issue