Tip: Create models.py from existing database

Featured

Tip: Create models.py from existing database

Ever wanted to use an old, existing, legacy database with a Django Project? That’s totally possible!

Django allows you to create a models.py file based on an existing database. To do this, you first have to make sure your database settings are properly specified in the settings.py file. Here’s an example of what the database settings in your settings.py file should look like:
DATABASE_ENGINE = ‘mysql’ # ‘postgresql_psycopg2′, ‘postgresql’, ‘mysql’, ’sqlite3′ or ‘oracle’.
DATABASE_NAME = ‘YourDataBaseName’ # Or path to database file if using sqlite3.
DATABASE_USER = ‘YourDataBaseUser’ # Not used with sqlite3.
DATABASE_PASSWORD = ’YourVerySecretPassword’ # Not used with sqlite3.
DATABASE_HOST = ” # Set to empty string for localhost. Not used with sqlite3.
DATABASE_PORT = ” # Set to empty string for default. Not used with sqlite3.

Then, from the command line, type:

python mysite/manage.py inspectdb > mysite/myapp/models.py

That should be all! Django created a models.py for you, based on the contents of the existing database.

Discussion

3 comments for “Tip: Create models.py from existing database”

  1. Awesome tip! Just what I needed.

    Posted by mitch | December 17, 2008, 8:52 am
  2. It Helps… Thanks…

    Posted by Nathaniel | January 29, 2009, 6:07 am
  3. Thanks, I was looking for this ;-) .

    About the generated models.py, do you imagine an post to explain how-to use it (using foreign keys, etc.)
    mike

    Posted by mike | August 26, 2010, 3:23 pm

Post a comment

Categories

  • No categories

Django Hosting:

DjangoDays recommends the following Django hosting provider: