Ticket #5563: boolean_no_null_validation.patch

File boolean_no_null_validation.patch, 3.2 KB (added by SamBull, 6 years ago)

patch to raise a validation error for BooleanField(null=True)

  • django/core/management/validation.py

    diff --git a/django/core/management/validation.py b/django/core/management/validation.py
    index 68bfb6c..31a5604 100644
    a b def get_validation_errors(outfile, app=None): 
    5151                    from PIL import Image
    5252                except ImportError:
    5353                    e.add(opts, '"%s": To use ImageFields, you need to install the Python Imaging Library. Get it at http://www.pythonware.com/products/pil/ .' % f.name)
     54            if isinstance(f, models.BooleanField) and getattr(f, 'null', False):
     55                e.add(opts, '"%s": BooleanFields cannot allow null values. Use NullBooleanField instead.' % f.name)
    5456            if f.choices:
    5557                if isinstance(f.choices, basestring) or not is_iterable(f.choices):
    5658                    e.add(opts, '"%s": "choices" should be iterable (e.g., a tuple or list).' % f.name)
  • tests/modeltests/invalid_models/models.py

    diff --git a/tests/modeltests/invalid_models/models.py b/tests/modeltests/invalid_models/models.py
    index 2817208..0fa4a7c 100644
    a b class FieldErrors(models.Model): 
    1414    choices2 = models.CharField(max_length=10, choices=[(1,2,3),(1,2,3)])
    1515    index = models.CharField(max_length=10, db_index='bad')
    1616    field_ = models.CharField(max_length=10)
     17    nullbool = models.BooleanField(null=True)
    1819class Target(models.Model):
    1920    tgt_safe = models.CharField(max_length=10)
    invalid_models.fielderrors: "choices2": "choices" should be a sequence of two-tu 
    190191invalid_models.fielderrors: "choices2": "choices" should be a sequence of two-tuples.
    191192invalid_models.fielderrors: "index": "db_index" should be either None, True or False.
    192193invalid_models.fielderrors: "field_": Field names cannot end with underscores, because this would lead to ambiguous queryset filters.
     194invalid_models.fielderrors: "nullbool": BooleanFields cannot allow null values. Use NullBooleanField instead.
    193195invalid_models.clash1: Accessor for field 'foreign' clashes with field 'Target.clash1_set'. Add a related_name argument to the definition for 'foreign'.
    194196invalid_models.clash1: Accessor for field 'foreign' clashes with related m2m field 'Target.clash1_set'. Add a related_name argument to the definition for 'foreign'.
    195197invalid_models.clash1: Reverse query name for field 'foreign' clashes with field 'Target.clash1'. Add a related_name argument to the definition for 'foreign'.
  • tests/regressiontests/serializers_regress/models.py

    diff --git a/tests/regressiontests/serializers_regress/models.py b/tests/regressiontests/serializers_regress/models.py
    index e26b8df..95119d4 100644
    a b from django.contrib.contenttypes.models import ContentType 
    1111from django.contrib.localflavor.us.models import USStateField, PhoneNumberField
    1313# The following classes are for testing basic data
    14 # marshalling, including NULL values.
     14# marshalling, including NULL values, where allowed.
    1616class BooleanData(models.Model):
    17     data = models.BooleanField(null=True)
     17    data = models.BooleanField()
    1919class CharData(models.Model):
    2020    data = models.CharField(max_length=30, null=True)
Back to Top