0

According to this answer (Django script to access model objects without using manage.py shell) before I can use Django models in my standalone python script it is enough to point it to the project's settings file:

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_project.settings")
from my_app.models import MyModel

This results in the error:

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

This however works just fine:

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_project.settings")
import django
django.setup()
from my_app.models import MyModel

I use Django 2.1.7

Is it required now to use django.setup() in each script where I use Django models?

Philipp Chapkovski
  • 1,695
  • 2
  • 20
  • 32

1 Answers1

1

In short, yes. You are required to call django.setup if you want to use Django features.

More on this doc.

foobarna
  • 630
  • 3
  • 11