Home > Django > Integrating Django with a legacy database

Integrating Django with a legacy database

After connect to your database.

Step 1:

python manage.py inspectdb

Outputs general models from your db that u defined it in settings file.

Step 2:

python manage.py inspectdb > ./your_app/models.py

creates models.py file from the output in *nix systems.

Step 3:

You’ll have to do the following manually to clean this up:

Rearrange models order

Make sure each model has one field with primary_key=True

Remove ‘managed = False’ lines if you wish to allow Django to create and delete the table

Step 4:

But it will have some problems like same table will more than one foreign key fields, those filed names should not have the related_name because with out related_name you django can’t figure out which field is connects to foreign key filed. To check this run the following command.

You will see the django-admin.py sqlcustom [appname] but dont run this command because it wont take the

python manage.py sqlcustom [appname]

 

References:

Stackoverflow

Djangoproject

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: