4

I know this exists

django-admin.py inspectdb > models.py

However, is there an easy way to limit it? Without manually deleting what I don't want.

I'm connecting to a database that has over one hundred tables, but I only want models of about 4 or 5. Is there an easy way to generate models from a few given tables?

They are quite big tables, so I don't fancy typing them all out either.

colinjwebb
  • 4,362
  • 7
  • 31
  • 35
  • Checked the source of Django 1.1 and didn't find a way to do this. Depending on your database engine, you maybe able to restrict the tables that Django sees or simply create a dummy database with the schemas of the tables that you want to inspect. – Ayman Hourieh Mar 30 '10 at 18:49
  • Its an Oracle database - not sure how I'd restrict the tables it sees though? – colinjwebb Mar 30 '10 at 22:04
  • I'm not familiar with the permissions system in Oracle, so I don't know. What about the second idea? It should be pretty easy to recreate the schemas of those 4-5 tables in a separate database. – Ayman Hourieh Mar 30 '10 at 23:16
  • It probably would be easy to recreate the schemas, but it seems like some hassle I don't want to go through. I just inspected the db and manually deleted the extra models this morning. – colinjwebb Mar 31 '10 at 11:55

4 Answers4

7

I just did this myself, also with Oracle. It's possible - but not pretty.

Assuming you know the names of the tables you want -

open django/db/backends/oracle/introspection.py. There is a function get_table_list:

def get_table_list(self, cursor):
    "Returns a list of table names in the current database."
    cursor.execute("SELECT TABLE_NAME FROM USER_TABLES")
    return [row[0].lower() for row in cursor.fetchall()]

Just replace it with something like

def get_table_list(self, cursor):
    names = ['mytable1', 'mytable2', 'mytable3']
    return names

Then run your inspectdb and it will be a lot more managable :)

pfctdayelise
  • 5,115
  • 3
  • 32
  • 52
  • newer version of Django: def get_table_list(self, cursor): names = [ TableInfo('table1', 't'), TableInfo('table_2', 't'), ..... ] return names – chachra Sep 29 '20 at 03:42
0

Do not use syncdb > models.py. It's not a good practice. Make your models manually and add managed=False to it. If you will not add it your all database tables can be deleted via single command. After creating your models then run the syncdb so that tables are linked.

JJD
  • 50,076
  • 60
  • 203
  • 339
ha22109
  • 8,036
  • 13
  • 44
  • 48
  • 1
    Surely good practice would just be to check models.py afterwards? Typing everything out would just be tedious – colinjwebb Apr 01 '10 at 12:14
0

Following solution given by @pfctdayelise

For django 1.8 mysql backend

open django/db/backends/mysql/introspection.py and find function get_table_list:

def get_table_list(self, cursor):
    cursor.execute("SHOW FULL TABLES")
    return [TableInfo(row[0], {'BASE TABLE': 't', 'VIEW': 'v'}.get(row[1]))
            for row in cursor.fetchall()]

Replace it with something like

def get_table_list(self, cursor):
    names = [TableInfo('mytable1', 't')]
    return names

To decide whether the second argument to TableInfo is t or v, run the mysql query SHOW FULL TABLES and find out your table_type if it is a BASE_TABLE then second argument is t else v

Then run

python manage.py inspectdb > models.py
Venkat Kotra
  • 10,413
  • 3
  • 49
  • 53
0

Starting in Django 1.10, the inspectdb command takes an optional list of tables on the command line that limits which tables will be inspected.

jensq
  • 146
  • 6