Skip to content Skip to sidebar Skip to footer

Import App In Django Project

I had a problem with an import of an app in another app in my django project. I know there are severals question/asnwsers on this subject, and believe me I read a lot of them, even

Solution 1:

Example project (that works).

Project structure

test_project/
  test_project/
    settings.py
    urls.py
    wsgi.py 
  app1/
    models.py
  app2/
    models.py

app1/models.py

from django.db import models

classRestaurant(models.Model):
    name = models.CharField(max_length=255)

app2/models.py

from django.db import models
from app1.models import Restaurant

classWaiter(models.Model):
    restaurant = models.ForeignKey(Restaurant)
    name = models.CharField(max_length=255)

In your case both from ..crew.models import MyClass and from crew.models import MyClass should work. My guess is that you (or PyCharm) try to run some file or import module when you (or PyCharm) are in app directory. Current directory (whatever it means) should be was (that one created by django-admin.exe. Hope it helps.

Solution 2:

In django projects you do not need to give the path from the root to project directory.Also the BASE_DIR is path to your django project this when you import any app in django you just import it using its name.

So your import will be.

from crew.modelsimportMyClass

Solution 3:

I think that helps you! I struggled the same problem for couple of days. I had all the init.py files in place. I ended up having to set the sources root to project_dir:

Right-click on project_dir, Mark Directory as > Sources Root

And it helped!

Post a Comment for "Import App In Django Project"