0

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.

Sam B.
  • 2,703
  • 8
  • 40
  • 78
  • I'm not sure where that's coming from but shouldn't you better create a real django project and remove (once it's achieved) from there what you don't need (web server..) from it ? – John Doe Jan 05 '19 at 12:29
  • @JohnDoe I do have an actual django model with a file called path in the main project directory. since I'm running them separately i.e. one one terminal `python manage.py runserver` and on another `python path.py` the path creates the sockets. And since some of the data created would best be stored in a model I need the path file to access the model. – Sam B. Jan 05 '19 at 12:44
  • You should import settings always from `settings.conf`. In your settings.py simply write: `DATABASES = {..}`. You don't need a settings import there. Go through https://docs.djangoproject.com/en/2.1/intro/tutorial02/ to fix your architecture. You're `django.setup()`-code looks fine. Kind regards. – Tobias Ernst Jan 05 '19 at 13:52

1 Answers1

0

I guess simple solution - almost empty django project. Here is the old project that gives you access to django ORM from python https://github.com/askaliuk/django-orm-standalone but i am not shure it works.