3

Is it possible to import models from apps in different Django projects?

I hope to move some common models in a base projects from which every child projects can share the same data in these common models.

Edit

I have to place

from baseproject.appname.models import basemodel

before

os.environ['DJANGO_SETTINGS_MODULE'] = 'childproject.settings'
from django.conf import settings

in child project to access the data in base model correctly.

1 Answer 1

3

Yes. You can turn a project-specific app into a standard Python package by moving it to site-packages (or wherever your Python install expects its modules) and breaking any links from it to other apps in the project. You can then import it as you would any Python module, in any project.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi Ignacia, the base project is already installed site-packages folder. The import statement "from baseproject.app.models import basemodel" works but when I tried to access data in basemodel, it keep showing "Table childproject.baseproject_basemodel doesn't exists".
It's not necessary to put the whole project in site-packages, just the app. Did you remember to add it to INSTALLED_APPS and do a syncdb?
@Ignacio, I just found the reason.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.