Commit 22c6497f authored by Alasdair Nicol's avatar Alasdair Nicol Committed by Tim Graham
Browse files

Fixed #20895 -- Made check management command warn if a BooleanField does not have a default value

Thanks to Collin Anderson for the suggestion and Tim Graham for
reviewing the patch.
parent 55339a76
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -442,6 +442,7 @@ answer newbie questions, and generally made Django that much better:
    Gopal Narayanan <gopastro@gmail.com>
    Fraser Nevett <mail@nevett.org>
    Sam Newman <http://www.magpiebrain.com/>
    Alasdair Nicol <http://al.sdair.co.uk/>
    Ryan Niemeyer <https://profiles.google.com/ryan.niemeyer/about>
    Filip Noetzel <http://filip.noetzel.co.uk/>
    Afonso Fernández Nogueira <fonzzo.django@gmail.com>
+1 −1
Original line number Diff line number Diff line
@@ -40,7 +40,7 @@ class Track(models.Model):
    def __str__(self): return self.name

class Truth(models.Model):
    val = models.BooleanField()
    val = models.BooleanField(default=False)
    objects = models.GeoManager()

if not spatialite:
+28 −1
Original line number Diff line number Diff line
from __future__ import unicode_literals

from django.db import models

def check_test_runner():
    """
@@ -24,6 +25,31 @@ def check_test_runner():
        ]
        return ' '.join(message)

def check_boolean_field_default_value():
    """
    Checks if there are any BooleanFields without a default value, &
    warns the user that the default has changed from False to Null.
    """
    fields = []
    for cls in models.get_models():
        opts = cls._meta
        for f in opts.local_fields:
            if isinstance(f, models.BooleanField) and not f.has_default():
                fields.append(
                    '%s.%s: "%s"' % (opts.app_label, opts.object_name, f.name)
                )
    if fields:
        fieldnames = ", ".join(fields)
        message = [
            "You have not set a default value for one or more BooleanFields:",
            "%s." % fieldnames,
            "In Django 1.6 the default value of BooleanField was changed from",
            "False to Null when Field.default isn't defined. See",
            "https://docs.djangoproject.com/en/1.6/ref/models/fields/#booleanfield"
            "for more information."
        ]
        return ' '.join(message)


def run_checks():
    """
@@ -31,7 +57,8 @@ def run_checks():
    messages from all the relevant check functions for this version of Django.
    """
    checks = [
        check_test_runner()
        check_test_runner(),
        check_boolean_field_default_value(),
    ]
    # Filter out the ``None`` or empty strings.
    return [output for output in checks if output]
+4 −4
Original line number Diff line number Diff line
@@ -115,7 +115,7 @@ class ModelWithStringPrimaryKey(models.Model):
@python_2_unicode_compatible
class Color(models.Model):
    value = models.CharField(max_length=10)
    warm = models.BooleanField()
    warm = models.BooleanField(default=False)
    def __str__(self):
        return self.value

@@ -144,7 +144,7 @@ class Actor(models.Model):

@python_2_unicode_compatible
class Inquisition(models.Model):
    expected = models.BooleanField()
    expected = models.BooleanField(default=False)
    leader = models.ForeignKey(Actor)
    country = models.CharField(max_length=20)

@@ -376,7 +376,7 @@ class Link(models.Model):

class PrePopulatedPost(models.Model):
    title = models.CharField(max_length=100)
    published = models.BooleanField()
    published = models.BooleanField(default=False)
    slug = models.SlugField()


@@ -607,7 +607,7 @@ class PrePopulatedPostLargeSlug(models.Model):
    the javascript (ie, using THOUSAND_SEPARATOR ends up with maxLength=1,000)
    """
    title = models.CharField(max_length=100)
    published = models.BooleanField()
    published = models.BooleanField(default=False)
    slug = models.SlugField(max_length=1000)

class AdminOrderedField(models.Model):
+1 −1
Original line number Diff line number Diff line
@@ -64,7 +64,7 @@ class Store(models.Model):
class Entries(models.Model):
    EntryID = models.AutoField(primary_key=True, db_column='Entry ID')
    Entry = models.CharField(unique=True, max_length=50)
    Exclude = models.BooleanField()
    Exclude = models.BooleanField(default=False)


class Clues(models.Model):
Loading