From:http://www.czug.org/python/django/17.html
While Django is best suited to developing projects from scratch-the so-called "green domain" development-it is possible to integrate frameworks with legacy systems and databases, this chapter explains some
Integration Strategy
integration with legacy databases
The Django database layer generates SQL schemas--from Python code but for the legacy database you already have SQL schemas, in which case you need to
A database table write model already exists (for performance reasons, the Django database layer does not support non-working object-relational mappings through the run-time introspection database.
In order to use the database API, you need to write the model code, fortunately, Django comes with a helper tool that generates model code by reading your database table plan
This helper is called manage.py Inspectdb
using Inspectdb
The Inspectdb helper checks your settings file to point to the database, determines the Django model you represent for your table and prints the Python model code to standard output
Here is a typical zero-based legacy database process, only if Django is installed and you have a legacy database
1, create a Django project by running django-admin.py Startproject MySite (here MySite is your project name) and we'll use mysit in the example
e As the project name
2, edit the settings file in the project mysite/settings.py to tell Django what your database connection parameters are and what the database name is, in particular,
You will want to specify Database_name,database_engine,database_user,database_password,database_host and Database_prot settings
3, by running Python mysite/manage.py startapp MyApp (where MyApp is your app name) to create a Django app in your project, we
Use MyApp as the project name
4. Run the command Python mysite/manage.py Inspectdb, which will examine the tables in the database_name database and print the model classes for each table to see the output
To see what INSPECTDB can do.
5. Save the output to the models.py file in your app by using the callout shell output redirection:
Python mysite/manage.py inspectdb > mysite/myapp/models.py
6, edit the mysite/myapp/models.py file, organize the generated model and do the customizations you need, we will give some hints in the next section
organizing the resulting model
As you might expect, the database is not perfect, you will need to do some light-weight results for the model code, here are some instructions for dealing with the generated model:
1, each database table is converted to a model class-that is, there is a one-to mapping between the database table and the model class, which means that you will need to have any many-to-many
Linked table reorganization model for Manytomanyfield objects
2, each generated model has a property for each domain-including the ID primary key field, however, if the model does not have a primary key, the call to Django automatically adds the ID primary key field
This way, if you are very anus, you will like to remove any rows like this because they are redundant:
id = models. Integerfield (Primary_key=true)
3, the type of each domain (such as Charfield,datefield) is determined by looking at the database column type (such as varchar,date) if Inspectdb cannot map a
The type of the column to the Model field, which uses TextField and inserts a python comment near the field in the resulting model, "this field type is guessed", beware of it, if necessary
The corresponding change field type
4, if the database column name is a python reserved word (such as pass,class or for), Inspectdb will add ' _field ' to the property name, for example, if a table has a
column for, the resulting model will have a domain For_field,db_column property set to ' for '. Inspectdb Insert Python comment ' field is renamed because it is
Python reserved word ' to near domain
5, if your database contains tables that refer to other tables (most databases do), you may need to rearrange the order of the generated models to refer to other models
For example, Model Foo has a foreignkey to model bar, then the model bar should be defined before model Foo
6,inspectdb for Postgresql,mysql and SQLite to detect the primary key, that is, it is inserted in the appropriate place primary_key=true, for other databases, you will need to
To insert primary_key=true for at least one domain in each model, because the Django model needs to have a primary=true domain
7, foreign key detection works only with PostgreSQL and certain types of MySQL tables, in other cases the foreign key field will be born as Integerfield (assuming the foreign key column is int)