Django

Code

Changeset 1518

Show
Ignore:
Timestamp:
12/01/05 00:32:25 (3 years ago)
Author:
adrian
Message:

Fixed #971 -- inspectdb for SQLite now introspects field types.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/core/db/backends/sqlite3.py

    r1484 r1518  
    186186} 
    187187 
    188 DATA_TYPES_REVERSE = {} 
     188# Maps SQL types to Django Field types. Some of the SQL types have multiple 
     189# entries here because SQLite allows for anything and doesn't normalize the 
     190# field type; it uses whatever was given. 
     191BASE_DATA_TYPES_REVERSE = { 
     192    'bool': 'BooleanField', 
     193    'boolean': 'BooleanField', 
     194    'smallint': 'SmallIntegerField', 
     195    'smallinteger': 'SmallIntegerField', 
     196    'int': 'IntegerField', 
     197    'integer': 'IntegerField', 
     198    'text': 'TextField', 
     199    'char': 'CharField', 
     200    'date': 'DateField', 
     201    'datetime': 'DateTimeField', 
     202    'time': 'TimeField', 
     203
     204 
     205# This light wrapper "fakes" a dictionary interface, because some SQLite data 
     206# types include variables in them -- e.g. "varchar(30)" -- and can't be matched 
     207# as a simple dictionary lookup. 
     208class FlexibleFieldLookupDict: 
     209    def __getitem__(self, key): 
     210        key = key.lower() 
     211        try: 
     212            return BASE_DATA_TYPES_REVERSE[key] 
     213        except KeyError: 
     214            import re 
     215            m = re.search(r'^\s*(?:var)?char\s*\(\s*(\d+)\s*\)\s*$', key) 
     216            if m: 
     217                return ('CharField', {'maxlength': m.group(1)}) 
     218            raise KeyError 
     219 
     220DATA_TYPES_REVERSE = FlexibleFieldLookupDict() 
  • django/trunk/django/core/management.py

    r1517 r1518  
    592592                else: 
    593593                    field_type_was_guessed = False 
     594 
     595                # This is a hook for DATA_TYPES_REVERSE to return a tuple of 
     596                # (field_type, extra_params_dict). 
     597                if type(field_type) is tuple: 
     598                    field_type, extra_params = field_type 
     599                else: 
     600                    extra_params = {} 
     601 
     602                if field_type == 'CharField' and row[3]: 
     603                    extra_params['maxlength'] = row[3] 
     604 
    594605                field_desc = '%s = meta.%s(' % (column_name, field_type) 
    595                 if field_type == 'CharField': 
    596                     field_desc += 'maxlength=%s' % (row[3]) 
     606                field_desc += ', '.join(['%s=%s' % (k, v) for k, v in extra_params.items()]) 
    597607                field_desc += ')' 
    598608                if field_type_was_guessed: 
  • django/trunk/docs/django-admin.txt

    r1484 r1518  
    8888 
    8989``inspectdb`` works with PostgreSQL, MySQL and SQLite. Foreign-key detection 
    90 only works in PostgreSQL. In SQLite, it cannot detect column types; it'll 
    91 use ``TextField`` for each column. 
     90only works in PostgreSQL. 
    9291 
    9392install [modelmodule modelmodule ...]