Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sharing database table between two django projects

I have two different Django projects that are meant to run in parallel and do pretty different things.

However they need to share a common database table, the Client table..

Both projects contains multiple apps that needs to contain foreign keys mapped to that Client model..

I'm not sure what would be the best approach..

like image 219
h3. Avatar asked Jun 25 '10 03:06

h3.


2 Answers

Assuming both projects are working on the same db, just import the model you want to reference to.

from first_project.some_app.models import Client, OtherSharedModel

class SomeModelInSecondProject(models.Model):
    client = models.ForeignKey(Client)
like image 143
zalew Avatar answered Sep 29 '22 08:09

zalew


Unfortunately, Django's support for multiple databases does not support cross-database relations. You could fake this on one of the systems (ie. have the table referenced, but handle the key refs yourself), but you would need to be very careful to document what you are doing to make sure you maintain referential integrity in the app that is 'faking' it.

like image 36
Peter Rowell Avatar answered Sep 29 '22 10:09

Peter Rowell