62👍
This has changed in Django 1.7
import os
import django
from myapp import models
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
django.setup()
print models.MyModel.objects.get(pk=1)
28👍
To expand on miki725’s answer, if before you had
from django.core.management import setup_environ
import fooproject.settings as settings
setup_environ(settings)
just replace with
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "fooproject.settings")
from django.conf import settings
and your settings will loaded as settings.
For Django 1.7+, see the solution from Ben Davis.
- [Django]-How to override css in Django Admin?
- [Django]-Django+Postgres: "current transaction is aborted, commands ignored until end of transaction block"
- [Django]-How to query directly the table created by Django for a ManyToMany relation?
21👍
Django 1.4 release notes officially recommend to use django.conf.settings.configure()
to set the settings. This is nice for small scripts for which you need to do everything the “pythonic” way. If however you have a bigger project, I like to use the Django approach which is to have a separate settings module/package and then its path in DJANGO_SETTINGS_MODULE
environment variable. This is the approach which is used in manage.py
:
# manage.py
# ...
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "fooproject.settings")
# ...
Release docs can be found here.
- [Django]-Django Model Fields Indexing
- [Django]-OneToOneField() vs ForeignKey() in Django
- [Django]-Django Queryset with year(date) = '2010'
2👍
To add to what Leandro N said (thank you, Leandro!), you have to add django.setup().
For example, my code:
import os, django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
django.setup()
from app.models import ModelA, ModelB
FYI, I’m on Django 1.10.
- [Django]-Django: Redirect to previous page after login
- [Django]-Django MultiValueDictKeyError error, how do I deal with it
- [Django]-Is virtualenv recommended for django production server?
2👍
I am using Django 2.0 and Ben Davis is close but I could not get it to work. What seems to have worked for me is
import os
import sys
import django
sys.path.append("/path/to/django/project") #only required if project not already in path
os.environ.setdefault('DJANGO_SETTINGS_MODULE','myapp.settings')
django.setup() #run django.setup BEFORE importing modules to setup the environ
from myapp.models import Thingy
t=Thingly.objects.all()
print(t)
- [Django]-Python/Django: log to console under runserver, log to file under Apache
- [Django]-How to limit the maximum value of a numeric field in a Django model?
- [Django]-How can I fill up form with model object data?
1👍
Disclaimer: I’m using Django 1.8
Just adding my 2 cents: I’ve tried some of the solutions presented in this page, but they did’t worked at all. I just came up with a solution that I’m sharing below.
Make sure you are in the root folder of your project. The same where manage.py
is located.
#make sure basic imports are in place
>>> import os,django
#set the specific django settings file
>>> os.environ.setdefault("DJANGO_SETTINGS_MODULE", "main.settings.base")
# import your modules and use them just like you do in your
# web apps
>>> from apps.customers import models as c
# normal usage... for instance:
>>> dir(c)
['You', 'Will', 'See', 'Your', 'Models', ... ]
- [Django]-How to customize the auth.User Admin page in Django CRUD?
- [Django]-H14 error in heroku – "no web processes running"
- [Django]-Django.contrib.gis.db.backends.postgis vs django.db.backends.postgresql_psycopg2