Jack Ha
Jack Ha

Reputation: 20951

Using 'old' database with django

I'm using a hand built (Postgres) database with Django. With "inspectdb" I was able to automatically create a model for it. The problem is that some tables have multiple primary keys (for many-to-many relations) and they are not accessible via Django.

What's the best way to access these tables?

Upvotes: 6

Views: 440

Answers (2)

Jason Baker
Jason Baker

Reputation: 198557

Django does have support for many-to-many relationships. If you want to use a helper table to manage this relationships, the ManyToManyField takes a through argument which specifies the table to use. You can't model anything terribly complex this way, but it is good for most simple applications.

Upvotes: 0

Haes
Haes

Reputation: 13116

There is no way to use composite primary keys in Django's ORM as of now (up to v1.0.2).

I can only think of three solutions/workarounds:

  1. There is a fork of django with a composite pk patch at github that you might want to try.
  2. You could use SQLAlchemy together with Django.
  3. You have to add a single field primary key field to those tables.

Upvotes: 4

Related Questions