0

Is it possible to use Django in a python script?

I think I saw something about it in the past, but I can't find any reference to it now. So, basically, I want to build a python script and access Django ORM.

Thanks for any help

2

2 Answers 2

2
import sys
import os
sys.path.insert(0, 'root directory of my project')
os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings'
import django
django.setup()
from myapp.models import MyModel

# now you can access MyModel objects just as if you were in a django view
Sign up to request clarification or add additional context in comments.

1 Comment

This is exactly what I need. Somehow I was not finding it. Thanks for helping.
1

TLDR:

if __name__ == '__main__':
    import django
    django.setup()

More info

Comments

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.