Standalone Django ORM - default settings not recognized

I have a typical Django web application. I want to create a separate project with its own virtualenv and use ORM to create new records in the database.

There are many resources on explaining how to do this, including this entry from the docs . Therefore, I wrote a script as follows:

# Add Django project directory to path, so that I can
# import the models
import sys
sys.path.append("/path/to/django/project")

# Import my settings file from the Django project
from django_proj import settings as django_settings

import django
from django.conf import settings
if not settings.configured:
    settings.configure(django_settings)
django.setup()

When I try to run it this way, I get the following error:

AttributeError: 'module' object has no attribute 'LOGGING_CONFIG'

I believe that this is due to the fact that I do not LOGGING_CONFIGexplicitly specify in my settings. I rely on the default value.

My configuration works when I run it through python manage.py. What is the problem with this extra setting that causes the problems?

Thanks!

+4
2

Try:

import os
import sys
import django
sys.path.append('/path/to/django_project')
#from django_project import *
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_project.settings')
django.setup()

. , .

+6

settings.configure(django_settings)

,

settings.configure(default_settings=django_settings)

, Django django_settings django.conf.global_settings. Django docs , , , LOGGING.

script Django - , @BigZ.

+3

Source: https://habr.com/ru/post/1607503/


All Articles