Saturday, January 5, 2019

Re: django use models from outside script

The simplest way to do this is implementing a custom admin command, which will be invoked then with :
./manage.py my_awesome_command


It will take care for you of setting the Django environment (i.e. updating the Python path so that the apps are included in it, applying settings and making your apps available to the command).

Have  a look here for detail : 
https://docs.djangoproject.com/en/2.1/howto/custom-management-commands/


Eric



From: django-users@googlegroups.com <django-users@googlegroups.com> on behalf of Samuel Muiruri <muiruri.samuel@gmail.com>
Sent: Saturday, January 5, 2019 1:52:04 PM
To: Django users
Subject: django use models from outside script
 
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][1] 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.


  [1]: https://stackoverflow.com/questions/35388637/runtimeerror-model-class-django-contrib-sites-models-site-doesnt-declare-an-ex

To be clear  I 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. 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d6c32914-e42f-48a3-bf59-c2835006e4bc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment