1

I want to be able to import my Django models into an external script to do things such as parse inputted URLs. I have tried creating a python script within the root folder of a Django app and simply importing the model as I would in views.py, however, I keep getting this error:

    django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

I have searched through the Django docs but so far, I haven't been able to find anything. Anybody got any ideas?

Thanks!

1
  • 1
    Such work is usually implemented as a management command. Commented Jan 5, 2020 at 5:39

1 Answer 1

1

You need to django.setup() before you can use Django models in a stand-alone script

import os
import django

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'your_app.settings')
django.setup()
Sign up to request clarification or add additional context in comments.

1 Comment

And I run this as its own 4 line script or do I include it it any stand-alone script?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.