Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django apps sharing a model

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?

like image 290
css3newbie Avatar asked Sep 24 '15 10:09

css3newbie


1 Answers

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
like image 178
Rahul Gupta Avatar answered Oct 05 '22 23:10

Rahul Gupta