views:

217

answers:

2

I have a Postgresql databese with data. I want to create a django app with that database.

How can i import the tables to django models and/or views?

+4  A: 

Read this: http://docs.djangoproject.com/en/dev/howto/legacy-databases/

There is a utility to generate models from your existing database. It works pretty well.

Fred Larson
Yes, django's `manage inspectdb` should be what you are searching for.
Michael
+3  A: 

If your database is not very simple -- or very well designed -- you'll find it a poor fit with Django.

While the reverse engineering works well, you may find that the original database design was flawed and you have lots of clumsy workarounds.

The question is one of "legacy software" that works with the old data model.

I'd suggest you do the following.

  1. Design the correct data model, using Django.

  2. Map the correct model to whatever it is you have.

  3. Write a conversion script that uses simple, direct SQL and the Django ORM to migrate data from non-Django-friendly to a better model.

    • If you have legacy software, you'll have to work out an appropriate data movement schedule.

    • If you don't have any legacy software, you'll run this conversion once.

S.Lott