diff options
| author | Adrian Holovaty <adrian@holovaty.com> | 2005-08-02 17:08:24 +0000 |
|---|---|---|
| committer | Adrian Holovaty <adrian@holovaty.com> | 2005-08-02 17:08:24 +0000 |
| commit | d9401b78f11b47c5daa95a4fa60925b3fed50203 (patch) | |
| tree | a12b4dd9c2d791b0cae428d70bb5fd012a64c753 /django/core/db/backends/postgresql.py | |
| parent | 1510ca1a80f811d6e681a880d7e49d413ce8afb4 (diff) | |
Added first stab at 'django-admin.py inspectdb', which takes a database name and introspects the tables, outputting a Django model. Works in PostgreSQL and MySQL. It's missing some niceties at the moment, such as detection of primary-keys and relationships, but it works. Refs #90.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@384 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/core/db/backends/postgresql.py')
| -rw-r--r-- | django/core/db/backends/postgresql.py | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/django/core/db/backends/postgresql.py b/django/core/db/backends/postgresql.py index 8f82e1b23f..cd8d9a064e 100644 --- a/django/core/db/backends/postgresql.py +++ b/django/core/db/backends/postgresql.py @@ -71,6 +71,17 @@ def get_date_trunc_sql(lookup_type, field_name): # http://www.postgresql.org/docs/8.0/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC return "DATE_TRUNC('%s', %s)" % (lookup_type, field_name) +def get_table_list(cursor): + "Returns a list of table names in the current database." + cursor.execute(""" + SELECT c.relname + FROM pg_catalog.pg_class c + LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace + WHERE c.relkind IN ('r', 'v', '') + AND n.nspname NOT IN ('pg_catalog', 'pg_toast') + AND pg_catalog.pg_table_is_visible(c.oid)""") + return [row[0] for row in cursor.fetchall()] + # Register these custom typecasts, because Django expects dates/times to be # in Python's native (standard-library) datetime/time format, whereas psycopg # use mx.DateTime by default. @@ -129,3 +140,19 @@ DATA_TYPES = { 'USStateField': 'varchar(2)', 'XMLField': 'text', } + +# Maps type codes to Django Field types. +DATA_TYPES_REVERSE = { + 16: 'BooleanField', + 21: 'SmallIntegerField', + 23: 'IntegerField', + 25: 'TextField', + 869: 'IPAddressField', + 1043: 'CharField', + 1082: 'DateField', + 1083: 'TimeField', + 1114: 'DateTimeField', + 1184: 'DateTimeField', + 1266: 'TimeField', + 1700: 'FloatField', +} |
