3

I am using django installed via cookiecutter(running inside virtualenv). On top of the file I try to setup django env. like this:

import os
import sys
import django
sys.path.append('/vagrant/my_project/my_project/')    
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings.local')
django.setup()
from models import KeyO # This one loads models.py from the same directory.

models.py looks like this:

from django.db import models
from django.conf import settings
from decimal import Decimal
from django.core.validators import MaxValueValidator
from djmoney.models.fields import MoneyField    

class KeyO(models.Model):
    keyword = models.CharField('Keyword', max_length=1000, unique=True)
    ...

And here is the error:

RuntimeError: Model class models.KeyO doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

I didn't modify apps.py and my settings file loads this app at the end. Any ideas?

Note: Using py3.5 and django 1.10 dev

Edit:

local settings: https://gist.github.com/daniel1943/6148f27e157bdb251666 common(main settings): https://gist.github.com/daniel1943/76c46fcfaaa996c9fd66

3
  • Can you add your settings.py file in your original question? I want to check it. Commented Jan 13, 2016 at 2:30
  • Added both to original question. Commented Jan 13, 2016 at 2:49
  • I've run into the same problem here... I think its Django 1.9 dev though. Can't get it to see config.settings.local Commented Feb 10, 2016 at 16:19

1 Answer 1

2

So here is the solution:

Instead of importing model with:

from models import KeyO

Do it like:

from myapp.models import KeyO

And the entire code to run standalone django 1.9+ scripts:

import os
import sys
import django
sys.path.append("/media/sf_VM_desktop/myproject/") #path to your settings file  
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
django.setup()

from keywords.models import KW
Sign up to request clarification or add additional context in comments.

Comments

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.