Django

Code

Ticket #5563: boolean_no_null_validation.patch

File boolean_no_null_validation.patch, 3.2 kB (added by SamBull, 1 year ago)

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

  • a/django/core/management/validation.py

    old new  
    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) 
  • a/tests/modeltests/invalid_models/models.py

    old new  
    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) 
    1718 
    1819class Target(models.Model): 
    1920    tgt_safe = models.CharField(max_length=10) 
     
    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'. 
  • a/tests/regressiontests/serializers_regress/models.py

    old new  
    1111from django.contrib.localflavor.us.models import USStateField, PhoneNumberField 
    1212 
    1313# The following classes are for testing basic data 
    14 # marshalling, including NULL values
     14# marshalling, including NULL values, where allowed
    1515 
    1616class BooleanData(models.Model): 
    17     data = models.BooleanField(null=True
     17    data = models.BooleanField(
    1818 
    1919class CharData(models.Model): 
    2020    data = models.CharField(max_length=30, null=True)