I have an external script that I want to have access to django's models primarily because it's an external implementation of sockets which is simple I want to see if this is possible.
This is the snippet of code I added below the settings.py file based on an answer on stackoverflow.
#Allow Django to be used externally
from django.conf import settings
settings.configure(
DATABASES={
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
},
#TIME_ZONE='America/Montreal',
)
and at the start of my separate script named path.py
I did the following imports
import django
import pixelart.settings
os.environ.setdefault(
"DJANGO_SETTINGS_MODULE",
"pixelart.settings"
)
django.setup()
from gallery.models import ThumbnailCache, Area, Color
Note: My django project is called pixelart with a model gallery I'm importing models from.
When I try to run the script I get the following error:
(pixelart) sam@sam-Lenovo-G51-35:~/code/pixelart$ python path.py
Traceback (most recent call last):
File "path.py", line 23, in <module>
from gallery.models import ThumbnailCache, Area, Color
File "/home/sam/code/pixelart/gallery/models.py", line 2, in <module>
from django.contrib.auth.models import User
File "/home/sam/code/envs/pixelart/lib/python3.6/site-packages/django/contrib/auth/models.py", line 3, in <module>
from django.contrib.contenttypes.models import ContentType
File "/home/sam/code/envs/pixelart/lib/python3.6/site-packages/django/contrib/contenttypes/models.py", line 134, in <module>
class ContentType(models.Model):
File "/home/sam/code/envs/pixelart/lib/python3.6/site-packages/django/db/models/base.py", line 95, in __new__
"INSTALLED_APPS." % (module, name)
RuntimeError: Model class django.contrib.contenttypes.models.ContentType doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.
One suggestion I've found based on this question is to add the sites to the installed apps and the site_id.
After trying this out and doing a migration the error remained the same.