6

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..

h3.
  • 10,688
  • 15
  • 51
  • 54

3 Answers3

5

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)
zalew
  • 10,171
  • 3
  • 29
  • 32
3

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.

Peter Rowell
  • 17,605
  • 2
  • 49
  • 65
  • I always love it when someone comes along 4+ years after the Q&A and downvotes the answer without adding anything at all to the conversation. Note to Anonymous Coward: if you follow the link in my answer you'll find that this is still true in Django Dev, and they tell you so explicitly. They even use scare-quotes around the word 'fake'. – Peter Rowell Nov 11 '14 at 19:41
  • 6
    I haven't downvoted you but I think the question could be interpreted as trying to have one database being shared in two different projects. It isn't necessarily related to cross-database relations. I just love it when someone else rants about something unrelated to the original question. – m0y Mar 15 '15 at 02:16
0

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).

Community
  • 1
  • 1
m0y
  • 137
  • 2
  • 9