Ticket #19341: 19341-1.diff

File 19341-1.diff, 3.5 KB (added by Claude Paroz, 12 years ago)

Detect NullBooleanField in inspectdb

  • django/core/management/commands/inspectdb.py

    diff --git a/django/core/management/commands/inspectdb.py b/django/core/management/commands/inspectdb.py
    index 936d5e8..b71bf01 100644
    a b class Command(NoArgsCommand):  
    108108                # Add 'null' and 'blank', if the 'null_ok' flag was present in the
    109109                # table description.
    110110                if row[6]: # If it's NULL...
    111                     extra_params['blank'] = True
    112                     if not field_type in ('TextField(', 'CharField('):
    113                         extra_params['null'] = True
     111                    if field_type == 'BooleanField(':
     112                        field_type = 'NullBooleanField('
     113                    else:
     114                        extra_params['blank'] = True
     115                        if not field_type in ('TextField(', 'CharField('):
     116                            extra_params['null'] = True
    114117
    115118                field_desc = '%s = models.%s' % (att_name, field_type)
    116119                if extra_params:
  • django/db/backends/sqlite3/introspection.py

    diff --git a/django/db/backends/sqlite3/introspection.py b/django/db/backends/sqlite3/introspection.py
    index 1df4c18..6075ef8 100644
    a b class FlexibleFieldLookupDict(object):  
    4545                return ('CharField', {'max_length': size})
    4646            raise KeyError
    4747
     48    def values(self):
     49        return self.base_data_types_reverse.values()
     50
     51
    4852class DatabaseIntrospection(BaseDatabaseIntrospection):
    4953    data_types_reverse = FlexibleFieldLookupDict()
    5054
  • tests/regressiontests/inspectdb/models.py

    diff --git a/tests/regressiontests/inspectdb/models.py b/tests/regressiontests/inspectdb/models.py
    index 4a66214..9125156 100644
    a b from django.db import models  
    33
    44class People(models.Model):
    55    name = models.CharField(max_length=255)
     6    is_admin = models.BooleanField()
     7    has_family = models.NullBooleanField()
    68
    79class Message(models.Model):
    810    from_field = models.ForeignKey(People, db_column='from_id')
  • tests/regressiontests/inspectdb/tests.py

    diff --git a/tests/regressiontests/inspectdb/tests.py b/tests/regressiontests/inspectdb/tests.py
    index 028d263..46b74d0 100644
    a b from __future__ import unicode_literals  
    33from django.core.management import call_command
    44from django.db import connection
    55from django.test import TestCase, skipUnlessDBFeature
     6from django.test.testcases import _deferredSkip
    67from django.utils.six import StringIO
    78
    89
     10def skipUnlessIntrospectBoolean():
     11    return _deferredSkip(
     12        lambda: 'BooleanField' not in connection.introspection.data_types_reverse.values(),
     13        "Database cannot introspect boolean fields")
     14
     15
    916class InspectDBTestCase(TestCase):
    1017
     18    @skipUnlessIntrospectBoolean()
     19    def test_boolean_inspection(self):
     20        out = StringIO()
     21        # Lets limit the introspection to the test target table
     22        call_command('inspectdb',
     23                     table_name_filter=lambda tn:tn=='inspectdb_people',
     24                     stdout=out)
     25        output = out.getvalue()
     26        self.assertIn("is_admin = models.BooleanField()", output)
     27        self.assertIn("has_family = models.NullBooleanField()", output)
     28
    1129    def test_stealth_table_name_filter_option(self):
    1230        out = StringIO()
    1331        # Lets limit the introspection to tables created for models of this
Back to Top