Django

Code

Changeset 2351

Show
Ignore:
Timestamp:
02/18/06 16:14:05 (3 years ago)
Author:
adrian
Message:

magic-removal: Merged to [2350]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/magic-removal/AUTHORS

    r2344 r2351  
    5252    Jeremy Dunck <http://dunck.us/> 
    5353    Clint Ecker 
     54    gandalf@owca.info 
    5455    Baishampayan Ghose 
    5556    Espen Grindhaug <http://grindhaug.org/> 
  • django/branches/magic-removal/django/core/management.py

    r2330 r2351  
    753753    yield "# You'll have to do the following manually to clean this up:" 
    754754    yield "#     * Rearrange models' order" 
    755     yield "#     * Add primary_key=True to one field in each model.
     755    yield "#     * Make sure each model has one field with primary_key=True
    756756    yield "# Feel free to rename the models, but don't rename db_table values or field names." 
    757757    yield "#" 
     
    767767        except NotImplementedError: 
    768768            relations = {} 
     769        try: 
     770            indexes = introspection_module.get_indexes(cursor, table_name) 
     771        except NotImplementedError: 
     772            indexes = {} 
    769773        for i, row in enumerate(introspection_module.get_table_description(cursor, table_name)): 
    770             column_name = row[0] 
     774            att_name = row[0] 
    771775            comment_notes = [] # Holds Field notes, to be displayed in a Python comment. 
    772776            extra_params = {}  # Holds Field parameters such as 'db_column'. 
    773777 
    774             if keyword.iskeyword(column_name): 
    775                 extra_params['db_column'] = column_name 
    776                 column_name += '_field' 
     778            if keyword.iskeyword(att_name): 
     779                extra_params['db_column'] = att_name 
     780                att_name += '_field' 
    777781                comment_notes.append('Field renamed because it was a Python reserved word.') 
    778782 
     
    780784                rel_to = relations[i][1] == table_name and "'self'" or table2model(relations[i][1]) 
    781785                field_type = 'ForeignKey(%s' % rel_to 
    782                 if column_name.endswith('_id'): 
    783                     column_name = column_name[:-3] 
     786                if att_name.endswith('_id'): 
     787                    att_name = att_name[:-3] 
    784788                else: 
    785                     extra_params['db_column'] = column_name 
     789                    extra_params['db_column'] = att_name 
    786790            else: 
    787791                try: 
     
    798802                    extra_params.update(new_params) 
    799803 
     804                # Add maxlength for all CharFields. 
    800805                if field_type == 'CharField' and row[3]: 
    801806                    extra_params['maxlength'] = row[3] 
    802807 
     808                # Add primary_key and unique, if necessary. 
     809                column_name = extra_params.get('db_column', att_name) 
     810                if column_name in indexes: 
     811                    if indexes[column_name]['primary_key']: 
     812                        extra_params['primary_key'] = True 
     813                    elif indexes[column_name]['unique']: 
     814                        extra_params['unique'] = True 
     815 
    803816                field_type += '(' 
    804817 
    805  
    806             field_desc = '%s = models.%s' % (column_name, field_type) 
     818            # Don't output 'id = meta.AutoField(primary_key=True)', because 
     819            # that's assumed if it doesn't exist. 
     820            if att_name == 'id' and field_type == 'AutoField(' and extra_params == {'primary_key': True}: 
     821                continue 
     822 
     823            field_desc = '%s = models.%s' % (att_name, field_type) 
    807824            if extra_params: 
    808825                if not field_desc.endswith('('): 
  • django/branches/magic-removal/django/db/backends/ado_mssql/introspection.py

    r1632 r2351  
    88    raise NotImplementedError 
    99 
     10def get_indexes(cursor, table_name): 
     11    raise NotImplementedError 
     12 
    1013DATA_TYPES_REVERSE = {} 
  • django/branches/magic-removal/django/db/backends/dummy/introspection.py

    r1803 r2351  
    44get_table_description = complain 
    55get_relations = complain 
     6get_indexes = complain 
    67 
    78DATA_TYPES_REVERSE = {} 
  • django/branches/magic-removal/django/db/backends/mysql/introspection.py

    r1689 r2351  
    1414def get_relations(cursor, table_name): 
    1515    raise NotImplementedError 
     16 
     17def get_indexes(cursor, table_name): 
     18    """ 
     19    Returns a dictionary of fieldname -> infodict for the given table, 
     20    where each infodict is in the format: 
     21        {'primary_key': boolean representing whether it's the primary key, 
     22         'unique': boolean representing whether it's a unique index} 
     23    """ 
     24    cursor.execute("SHOW INDEX FROM %s" % quote_name(table_name)) 
     25    indexes = {} 
     26    for row in cursor.fetchall(): 
     27        indexes[row[4]] = {'primary_key': (row[2] == 'PRIMARY'), 'unique': not bool(row[1])} 
     28    return indexes 
    1629 
    1730DATA_TYPES_REVERSE = { 
  • django/branches/magic-removal/django/db/backends/postgresql/introspection.py

    r1689 r2351  
    3838    return relations 
    3939 
     40def get_indexes(cursor, table_name): 
     41    """ 
     42    Returns a dictionary of fieldname -> infodict for the given table, 
     43    where each infodict is in the format: 
     44        {'primary_key': boolean representing whether it's the primary key, 
     45         'unique': boolean representing whether it's a unique index} 
     46    """ 
     47    # Get the table description because we only have the column indexes, and we 
     48    # need the column names. 
     49    desc = get_table_description(cursor, table_name) 
     50    # This query retrieves each index on the given table. 
     51    cursor.execute(""" 
     52        SELECT idx.indkey, idx.indisunique, idx.indisprimary 
     53        FROM pg_catalog.pg_class c, pg_catalog.pg_class c2, 
     54            pg_catalog.pg_index idx 
     55        WHERE c.oid = idx.indrelid 
     56            AND idx.indexrelid = c2.oid 
     57            AND c.relname = %s""", [table_name]) 
     58    indexes = {} 
     59    for row in cursor.fetchall(): 
     60        # row[0] (idx.indkey) is stored in the DB as an array. It comes out as 
     61        # a string of space-separated integers. This designates the field 
     62        # indexes (1-based) of the fields that have indexes on the table. 
     63        # Here, we skip any indexes across multiple fields. 
     64        if ' ' in row[0]: 
     65            continue 
     66        col_name = desc[int(row[0])-1][0] 
     67        indexes[col_name] = {'primary_key': row[2], 'unique': row[1]} 
     68    return indexes 
     69 
    4070# Maps type codes to Django Field types. 
    4171DATA_TYPES_REVERSE = { 
  • django/branches/magic-removal/django/db/backends/sqlite3/introspection.py

    r2273 r2351  
    1010 
    1111def get_relations(cursor, table_name): 
     12    raise NotImplementedError 
     13 
     14def get_indexes(cursor, table_name): 
    1215    raise NotImplementedError 
    1316 
  • django/branches/magic-removal/docs/django-admin.txt

    r2273 r2351  
    113113This feature is meant as a shortcut, not as definitive model generation. After 
    114114you run it, you'll want to look over the generated models yourself to make 
    115 customizations. In particular, you'll need to do this: 
    116  
    117     * Rearrange models' order, so that models that refer to other models are 
    118       ordered properly. 
    119     * Add ``primary_key=True`` to one field in each model. The ``inspectdb`` 
    120       doesn't yet introspect primary keys. 
     115customizations. In particular, you'll need to rearrange models' order, so that 
     116models that refer to other models are ordered properly. 
     117 
     118If you're using Django 0.90 or 0.91, you'll need to add ``primary_key=True`` to 
     119one field in each model. In the Django development version, primary keys are 
     120automatically introspected for PostgreSQL and MySQL, and Django puts in the 
     121``primary_key=True`` where needed. 
    121122 
    122123``inspectdb`` works with PostgreSQL, MySQL and SQLite. Foreign-key detection