Why This Happens
Django requires settings to be configured before using any Django features. If you import a model in a standalone script without configuring settings, this error occurs.
The Problem
from myapp.models import User
users = User.objects.all()The Fix
import os, django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
django.setup()
from myapp.models import User
users = User.objects.all()Step-by-Step Fix
- 1
Set DJANGO_SETTINGS_MODULE
Set the environment variable before importing Django.
- 2
Call django.setup()
Call after setting env var and before Django imports.
- 3
Use manage.py shell
For interactive work, use python manage.py shell.
Got the actual stack trace?
Paste it into our free AI explainer to get the cause and the fix for your specific case — no signup, nothing to install.
Explain my errorBugsly catches this automatically
Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.
Try Bugsly free