Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Standalone Script

Tags:

python

django

I am trying to access my Django (v1.10) app DB from another python script and having some trouble doing so.

This is my file and folder structure:

store  store    __init.py__    settings.py    urls.py    wsgi.py  store_app    __init.py__    admin.py    apps.py    models.py    ...  db.sqlite3  manage.py  other_script.py 

In accordance with Django's documentations my other_script.py looks like this:

import django from django.conf import settings  settings.configure(DEBUG=True) django.setup()  from store.store_app.models import MyModel 

But it generates a runtime error:

RunTimeError: Model class store.store_app.models.MyModel doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS. 

I should note that my INSTALLED_APPS list contains store_app as its last element.

If instead I try to pass a config like this:

import django from django.conf import settings from store.store_app.apps import StoreAppConfig  settings.configure(StoreAppConfig, DEBUG=True) django.setup()  from store.store_app.models import MyModel 

I get:

AttributeError: type object 'StoreAppConfig has no attribute  'LOGGING_CONFIG'. 

If I edit settings.py and add LOGGING_CONFIG=None I get another error about another missing attribute, and so on.

Any suggestions will be appreciated.

like image 850
Ben RR Avatar asked Sep 27 '16 11:09

Ben RR


People also ask

How do I use Django ORM standalone?

Now, since we want to use Django's ORM, we have to have Django running for which we need a manage.py file. Let's create a default manage.py by copying it from one of the past project or the code below. Make sure you define a correct relative import path for settings in Line 4. Let's create our settings.py file now.

What does Django setup () do?

It is used if you run your Django app as standalone. It will load your settings and populate Django's application registry. You can read the detail on the Django documentation.

Can Django run python scripts?

Although it is not recommended to run python scripts from Django shell, this is a great way to run background tasks. This is because when you run python scripts from Django shell, it gives you access to all models, views and functions defined in your Django project.


2 Answers

Try this

import sys, os, django sys.path.append("/path/to/store") #here store is root folder(means parent). os.environ.setdefault("DJANGO_SETTINGS_MODULE", "store.settings") django.setup()  from store_app.models import MyModel 

This script you can use anywhere in your system.

like image 63
itzMEonTV Avatar answered Sep 24 '22 02:09

itzMEonTV


This sounds like a great use case for Django Management commands. which has the added bonus you can run it scheduled from cron, direct from the commandline, or call from inside django. This gives the script full access to the same settings and environment variables as your main project.

If you move this into an appropriate directory - using store here as an example, not a suggestion, it should work.

store      management      __init__.py         commands         __init__.py          otherscript.py  
like image 24
Withnail Avatar answered Sep 20 '22 02:09

Withnail