How to import a model to a python file within my django project?
up vote
1
down vote
favorite
I have models.py and test.py files in my Django app folder.
My project is named strava, app: explorer_api
How do I import a model into 'test.py`?
I have tried:
from strava.explorer_api.models import Activity
from explorer_api.models import Activity
from .models import Activity
from models import Activity
but:
SystemError: Parent module '' not loaded, cannot perform relative
import
The structure:
/
admin.py
apps.py
collector.py
models.py
serializers.py
test.py
tests.py
urls.py
views.py
__init__.py
migrations/
0001_initial.py
__init__.py
__pycache__/
0001_initial.cpython-35.pyc
__init__.cpython-35.pyc
templates/
explorer_api/
index.html
save.html
__pycache__/
admin.cpython-35.pyc
collector.cpython-35.pyc
models.cpython-35.pyc
serializers.cpython-35.pyc
urls.cpython-35.pyc
views.cpython-35.pyc
__init__.cpython-35.pyc
python django
add a comment |
up vote
1
down vote
favorite
I have models.py and test.py files in my Django app folder.
My project is named strava, app: explorer_api
How do I import a model into 'test.py`?
I have tried:
from strava.explorer_api.models import Activity
from explorer_api.models import Activity
from .models import Activity
from models import Activity
but:
SystemError: Parent module '' not loaded, cannot perform relative
import
The structure:
/
admin.py
apps.py
collector.py
models.py
serializers.py
test.py
tests.py
urls.py
views.py
__init__.py
migrations/
0001_initial.py
__init__.py
__pycache__/
0001_initial.cpython-35.pyc
__init__.cpython-35.pyc
templates/
explorer_api/
index.html
save.html
__pycache__/
admin.cpython-35.pyc
collector.cpython-35.pyc
models.cpython-35.pyc
serializers.cpython-35.pyc
urls.cpython-35.pyc
views.cpython-35.pyc
__init__.cpython-35.pyc
python django
Do you have a__init__.pyfile in explorer_api?
– chet-the-wizard
Nov 19 at 20:45
Yes, I do.....................
– barciewicz
Nov 19 at 20:46
1
would you mind posting your file structure?
– Dap
Nov 19 at 20:46
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have models.py and test.py files in my Django app folder.
My project is named strava, app: explorer_api
How do I import a model into 'test.py`?
I have tried:
from strava.explorer_api.models import Activity
from explorer_api.models import Activity
from .models import Activity
from models import Activity
but:
SystemError: Parent module '' not loaded, cannot perform relative
import
The structure:
/
admin.py
apps.py
collector.py
models.py
serializers.py
test.py
tests.py
urls.py
views.py
__init__.py
migrations/
0001_initial.py
__init__.py
__pycache__/
0001_initial.cpython-35.pyc
__init__.cpython-35.pyc
templates/
explorer_api/
index.html
save.html
__pycache__/
admin.cpython-35.pyc
collector.cpython-35.pyc
models.cpython-35.pyc
serializers.cpython-35.pyc
urls.cpython-35.pyc
views.cpython-35.pyc
__init__.cpython-35.pyc
python django
I have models.py and test.py files in my Django app folder.
My project is named strava, app: explorer_api
How do I import a model into 'test.py`?
I have tried:
from strava.explorer_api.models import Activity
from explorer_api.models import Activity
from .models import Activity
from models import Activity
but:
SystemError: Parent module '' not loaded, cannot perform relative
import
The structure:
/
admin.py
apps.py
collector.py
models.py
serializers.py
test.py
tests.py
urls.py
views.py
__init__.py
migrations/
0001_initial.py
__init__.py
__pycache__/
0001_initial.cpython-35.pyc
__init__.cpython-35.pyc
templates/
explorer_api/
index.html
save.html
__pycache__/
admin.cpython-35.pyc
collector.cpython-35.pyc
models.cpython-35.pyc
serializers.cpython-35.pyc
urls.cpython-35.pyc
views.cpython-35.pyc
__init__.cpython-35.pyc
python django
python django
edited Nov 19 at 23:39
Dap
1,1111933
1,1111933
asked Nov 19 at 20:40
barciewicz
476311
476311
Do you have a__init__.pyfile in explorer_api?
– chet-the-wizard
Nov 19 at 20:45
Yes, I do.....................
– barciewicz
Nov 19 at 20:46
1
would you mind posting your file structure?
– Dap
Nov 19 at 20:46
add a comment |
Do you have a__init__.pyfile in explorer_api?
– chet-the-wizard
Nov 19 at 20:45
Yes, I do.....................
– barciewicz
Nov 19 at 20:46
1
would you mind posting your file structure?
– Dap
Nov 19 at 20:46
Do you have a
__init__.py file in explorer_api?– chet-the-wizard
Nov 19 at 20:45
Do you have a
__init__.py file in explorer_api?– chet-the-wizard
Nov 19 at 20:45
Yes, I do.....................
– barciewicz
Nov 19 at 20:46
Yes, I do.....................
– barciewicz
Nov 19 at 20:46
1
1
would you mind posting your file structure?
– Dap
Nov 19 at 20:46
would you mind posting your file structure?
– Dap
Nov 19 at 20:46
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
have a look at this Django script to access model objects without using manage.py shell
if strava is your project directory then
greater than or equal to Django 1.9-1.10
import sys, os, django
sys.path.append('full_path_to_strava_directory')
os.environ['DJANGO_SETTINGS_MODULE'] = 'strava.settings'
django.setup()
from strava.activity.models import Activity
pre Django 1.11
from strava.wsgi import application
from strava.activity models import Activity
I suggest to useos.environ.setdefault('DJANGO_SETTINGS_MODULE', 'strava.settings'), so that the environment variableDJANGO_SETTINGS_MODULEwill not be overriden if it already exists.
– Tobias Ernst
Nov 19 at 23:43
add a comment |
up vote
0
down vote
Do you have explorer_api inside your INSTALLED_APPS?
Also, leave just one import in tests.py:
from explorer_api.models import Activity
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53382318%2fhow-to-import-a-model-to-a-python-file-within-my-django-project%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
have a look at this Django script to access model objects without using manage.py shell
if strava is your project directory then
greater than or equal to Django 1.9-1.10
import sys, os, django
sys.path.append('full_path_to_strava_directory')
os.environ['DJANGO_SETTINGS_MODULE'] = 'strava.settings'
django.setup()
from strava.activity.models import Activity
pre Django 1.11
from strava.wsgi import application
from strava.activity models import Activity
I suggest to useos.environ.setdefault('DJANGO_SETTINGS_MODULE', 'strava.settings'), so that the environment variableDJANGO_SETTINGS_MODULEwill not be overriden if it already exists.
– Tobias Ernst
Nov 19 at 23:43
add a comment |
up vote
1
down vote
have a look at this Django script to access model objects without using manage.py shell
if strava is your project directory then
greater than or equal to Django 1.9-1.10
import sys, os, django
sys.path.append('full_path_to_strava_directory')
os.environ['DJANGO_SETTINGS_MODULE'] = 'strava.settings'
django.setup()
from strava.activity.models import Activity
pre Django 1.11
from strava.wsgi import application
from strava.activity models import Activity
I suggest to useos.environ.setdefault('DJANGO_SETTINGS_MODULE', 'strava.settings'), so that the environment variableDJANGO_SETTINGS_MODULEwill not be overriden if it already exists.
– Tobias Ernst
Nov 19 at 23:43
add a comment |
up vote
1
down vote
up vote
1
down vote
have a look at this Django script to access model objects without using manage.py shell
if strava is your project directory then
greater than or equal to Django 1.9-1.10
import sys, os, django
sys.path.append('full_path_to_strava_directory')
os.environ['DJANGO_SETTINGS_MODULE'] = 'strava.settings'
django.setup()
from strava.activity.models import Activity
pre Django 1.11
from strava.wsgi import application
from strava.activity models import Activity
have a look at this Django script to access model objects without using manage.py shell
if strava is your project directory then
greater than or equal to Django 1.9-1.10
import sys, os, django
sys.path.append('full_path_to_strava_directory')
os.environ['DJANGO_SETTINGS_MODULE'] = 'strava.settings'
django.setup()
from strava.activity.models import Activity
pre Django 1.11
from strava.wsgi import application
from strava.activity models import Activity
edited Nov 19 at 21:00
answered Nov 19 at 20:55
Dap
1,1111933
1,1111933
I suggest to useos.environ.setdefault('DJANGO_SETTINGS_MODULE', 'strava.settings'), so that the environment variableDJANGO_SETTINGS_MODULEwill not be overriden if it already exists.
– Tobias Ernst
Nov 19 at 23:43
add a comment |
I suggest to useos.environ.setdefault('DJANGO_SETTINGS_MODULE', 'strava.settings'), so that the environment variableDJANGO_SETTINGS_MODULEwill not be overriden if it already exists.
– Tobias Ernst
Nov 19 at 23:43
I suggest to use
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'strava.settings'), so that the environment variable DJANGO_SETTINGS_MODULE will not be overriden if it already exists.– Tobias Ernst
Nov 19 at 23:43
I suggest to use
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'strava.settings'), so that the environment variable DJANGO_SETTINGS_MODULE will not be overriden if it already exists.– Tobias Ernst
Nov 19 at 23:43
add a comment |
up vote
0
down vote
Do you have explorer_api inside your INSTALLED_APPS?
Also, leave just one import in tests.py:
from explorer_api.models import Activity
add a comment |
up vote
0
down vote
Do you have explorer_api inside your INSTALLED_APPS?
Also, leave just one import in tests.py:
from explorer_api.models import Activity
add a comment |
up vote
0
down vote
up vote
0
down vote
Do you have explorer_api inside your INSTALLED_APPS?
Also, leave just one import in tests.py:
from explorer_api.models import Activity
Do you have explorer_api inside your INSTALLED_APPS?
Also, leave just one import in tests.py:
from explorer_api.models import Activity
answered Nov 19 at 21:09
Walucas
1,21611224
1,21611224
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53382318%2fhow-to-import-a-model-to-a-python-file-within-my-django-project%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Do you have a
__init__.pyfile in explorer_api?– chet-the-wizard
Nov 19 at 20:45
Yes, I do.....................
– barciewicz
Nov 19 at 20:46
1
would you mind posting your file structure?
– Dap
Nov 19 at 20:46