Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Standalone Django ORM - default settings not recognized

I have a typical Django web app. I want to create a separate project, with its own virtualenv, and make use of the ORM to create new entries in the DB.

There are a lot of resources on explaining how to do this, including this entry from the docs. So 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 this is due to the fact that I don't specify a LOGGING_CONFIG explicitly in my settings. I rely on the default value.

My configuration clearly works when I run it via python manage.py. What is the issue with this additional setup that's causing problems?

Thanks!

like image 388
user1496984 Avatar asked Sep 15 '15 15:09

user1496984


1 Answers

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()

This worked for me. Hope it helps

like image 130
BigZ Avatar answered Oct 10 '22 10:10

BigZ