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 commo开发者_开发问答n 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..
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)
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.
I haven't tested it but another alternative, if you're sharing the same db and having both projects in the same server, is to just merge them into one project, organize their apps in different directories and if you must you can use two different setting files. Please see this question related to that: How to keep all my django applications in specific folder. It's just a different approach that doesn't require you to reference a different project (I'm not sure how recommendable that is).
精彩评论