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?
To do this, create a directory called /«project»/«app_name»/models , inside it put __init__.py (to declare it as a module) and then create your files inside there. You then need to import your file contents into the module in __init__.py . You should read about Python modules to understand this.
A Django application is a Python package that is specifically intended for use in a Django project. An application may use common Django conventions, such as having models , tests , urls , and views submodules.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With