Standalone script with Django

I am going to run external script by invoking Django module, db models etc.

Here the refernece link from django official docs.:

https://docs.djangoproject.com/en/2.0/topics/settings/

However, for the quote of website which is not working for me:

import django
from django.conf import settings
from myapp import myapp_defaults

settings.configure(default_settings=myapp_defaults, DEBUG=True)
django.setup()

# Now this script or any imported module can use any part of Django it needs.
from myapp import models

simply I am using following code instead:

#!/usr/bin/env python3

import os
import sys

project_path="/home/liboma/saltmgt/"
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "saltmgt.saltmgt.settings")
sys.path.append(project_path)
#os.chdir(project_path)
if __name__ == '__main__':
    import django
    django.setup()
    from autocd.models import *
    <<your scripts>>

which is working fine for me.

Enjoy 🙂

other refernece links which helps:

  •  http://www.b-list.org/weblog/2007/sep/22/standalone-django-scripts/
  • https://www.stavros.io/posts/standalone-django-scripts-definitive-guide/

 

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay

Leave a Reply

Your email address will not be published. Required fields are marked *