fiddlest
fiddlest

Reputation: 1302

django cannot import models from directory under app

I have dir structure like below picture. I would like to import models in thumbnail dir. However, I cannot get models. It just throw syntax invalid.

I am trying to import models.py in WebtoonServer from thumbnail/thumbnail.py

from WebtoonServer.models # models.py is in WebtoonServer dir

enter image description here

I tried this as well

import os
import sys
sys.path.append(os.path.abspath('../'))

from WebtoonServer.models

Here is my INSTALLED_APPS

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'WebtoonServer',
    'rest_framework',
]

Upvotes: 0

Views: 1162

Answers (2)

Amin Mir
Amin Mir

Reputation: 642

if for example you have Class1 in app1.modles and Class2 in app2.models the error throws when your try to import Class1 into app2.models and mistakenly try to import Class2 into app1.models

Upvotes: 0

almost a beginner
almost a beginner

Reputation: 1632

You can't just say where you're importing from, but not what you're importing, they both have to be specified.

Have you tried from WebtoonServer.models import <Class Name>??

I think this is most likely related to your setup, the above code should definitely work, and has worked on proper Django setups.

I suggest having a read here: http://python-notes.curiousefficiency.org/en/latest/python_concepts/import_traps.html

Describes some issues that may arise with wrong structure. I see your "manage.py" is in WebtoonServer.

Upvotes: 1

Related Questions