5

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?

css3newbie
  • 165
  • 1
  • 5
  • 1
    Can Django apps have no models at all? – css3newbie Sep 24 '15 at 14:37
  • 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. – Jeffrey Swan Jul 21 '17 at 23:47

2 Answers2

8

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
Rahul Gupta
  • 46,769
  • 10
  • 112
  • 126
0

Just import the model from the app that defines it to the app that is using it.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895