Use Django from a local Python script

I had a little trouble trying various information on the Web, so I made a note Checked with Django 1.10 and python 3.6.0 below

local.py


import sys
import os
import django

sys.path.append('/Path/to/your/project')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'rootdir.settings')  #My settings.py


def call():
    django.setup()
    from yourapp.models import YourModel

    objects = YourModel.objects.all()
    for obj in objects:
        print(obj)


if __name__ == '__main__':
    call()

django.setup() To from yourapp.models import YourModel If you don't do it first

django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.


 Is said.

`os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'rootdir.settings')`
 ↑ I felt like I was told something like ↓ in connection with this setting, but I forgot the details.

 
>```django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.```

-

>```django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.```

***
 Pycharm doesn't go to see the contents of sys.path.append
 At the right end of the Preferences-> Project: yourproject-> Project Structure screen
 Specify the project with Add Content Root.



Recommended Posts

Use Django from a local Python script
Use thingsspeak from python
Use fluentd from python
Use MySQL from Python
Use MySQL from Python
Use mecab-ipadic-neologd from python
Run a Python script from a C # GUI application
Create a New Todoist Task from Python Script
"Python Kit" that calls a Python script from Swift
Call a Python script from Embedded Python in C ++ / C ++
Run a Python file from html using Django
Run a python script from excel (using xlwings)
[Python] Use a string sequence
Run illustrator script from python
Steps from installing Python 3 to creating a Django app
Use django model from interpreter
Python script to create a JSON file from a CSV file
Use e-Stat API from Python
How to use NUITKA-Utilities hinted-compilation to easily create an executable file from a Python script
How to run a Python program from within a shell script
Use pymol as a python library
Call a Python function from p5.js.
Use Stanford Core NLP from Python
Touch a Python object from Elixir
Django: Import a class from a string
Create new application use python, django
Read and use Python files from Python
Forcibly use Google Translate from python
python / Make a dict from a list.
Use blender as a python module
Use kabu Station® API from Python
Use the Flickr API from Python
Launch a Python script as a service
Use fastText trained model from Python
Write a batch script with Python3.5 ~
Use Google Analytics API from Python
Use print in a Python2 lambda expression
Use PostgreSQL data type (jsonb) from Python
Execute a script from Jupyter to process
Run a local script on a remote host
Use machine learning APIs A3RT from Python
Send a message from Python to Slack
I want to use jar from python
I have a question! (Python, django) Easy
Run the Python interpreter in a script
Use BigQuery from your local Jupyter Notebook
Use Google Cloud Vision API from Python
[Blender] Use OpenGL from inside the script
Manipulate BigQuery tables from a Python client
Use C ++ functions from python with pybind11
Use Python3's Subprocess.run () in a CGI script
Call a command from Python (Windows version)
How to run a Maya Python script
Connect to postgreSQL from Python and use stored procedures in a loop.
Send a message from Slack to a Python server
Edit Excel from Python to create a PivotTable
Script python file
How to open a web browser from python
Firebase: Use Cloud Firestore and Cloud Storage from Python
Create a C array from a Python> Excel sheet
python script skeleton