I Django can 2 apps share 1 model, or 2 apps must define the same model inside. so app1 and app1 can have have same products model inside them for example?
Django apps sharing a model
Asked Answered
Can Django apps have no models at all? –
Overplay
Absolutely. If you have no data to store or display in a database, such as if you just wanted to serve static content like images and css, you could easily do this with no models. If you have a homepage and apps, you could host them together with django, the homepage "app" possibly having no models. –
Idoux
Yes, app1
and app2
can share the same model. You need to import it wherever you want to use it.
Lets say your project structure is like below having 2 apps app1
and app2
.
my_project/
manage.py
my_project/
__init__.py
settings.py
urls.py
wsgi.py
app1/
__init__.py
admin.py
migrations/
__init__.py
models.py
tests.py
views.py
app2/
__init__.py
admin.py
migrations/
__init__.py
models.py
tests.py
views.py
Then to use the models defined in app1/models.py
in app2
, you just need to do:
from app1.models import MyModel # import the model
Just import the model from the app that defines it to the app that is using it.
© 2022 - 2024 — McMap. All rights reserved.