Reputation: 832
I've never done such thing so i'm not sure what would be the best approach for solving this problem:
I have two Django projects:
root/
project1/
manage.py
project1/
models.py
urls.py
...
project2/
manage.py
project2/
models.py
urls.py
...
Those projects use same database, they have around 10 models (database tables) each and some of the models overlap: Project1 needs ForeignKey from one fo the Project2's models, but also Project2 needs ForeignKey from one of the Project1's models:
Project1:
class Area_model(models.Model):
name = models.CharField(max_length=25)
url = models.CharField(max_length=25)
class Question_model(models.Model):
text = models.TextField(max_length=1000)
date = models.CharField(max_length=40)
answer = models.SmallIntegerField()
...
employee = models.ForeignKey(Employee_model)
Project2:
class Employee_model(models.Model):
name = models.CharField(max_length=15)
adress = models.CharField(max_length=15)
area = models.ForeignKey(Area_model)
I tried to import project1.models into project2's models.py but it says 'unknown module'. I need to import project1 to project2 and reverse, is that going to be a problem? (circular reference?) If so, how can i accomplish this in some other way?
Upvotes: 8
Views: 6834
Reputation: 65
In your WSGI.py, add the path of your second project to the sys.path by sys.path.append('/root')
.
In your settings.py of the first project, add 'project2.app2'
to the INSTALLED_APPS
list:
INSTALLED_APPS = [
...
'app1',
'project2.app2',
...
]
And then you should be able to easily import the models of your second project by using from project2.project2.models import *
At least that is how it worked for me.
Upvotes: 3
Reputation: 117
Try adding the path of the other project to your PYTHONPATH and then adding the app of the first project to settings.py installed apps of second project. Do not forget to import the app first and you will have to import the models of the first app in models.py of the second app. I am not sure if it will work as I have not tested it but you may give it a try.
Upvotes: 0