Skip to content

Instantly share code, notes, and snippets.

@noopurphalak
Last active December 14, 2023 05:35
Show Gist options
  • Save noopurphalak/b3ae80b3be941e6c97ef0a135c1c8861 to your computer and use it in GitHub Desktop.
Save noopurphalak/b3ae80b3be941e6c97ef0a135c1c8861 to your computer and use it in GitHub Desktop.
# A script that's needed to setup django if it's not already running on a server.
# Without this, you won't be able to import django modules
import django
import os
import sys
# Find the project base directory
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Add the project base directory to the sys.path
# This means the script will look in the base directory for any module imports
# Therefore you'll be able to import analysis.models etc
sys.path.insert(0, BASE_DIR)
# The DJANGO_SETTINGS_MODULE has to be set to allow us to access django imports
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "src.settings")
# Allow queryset filtering asynchronously when running in a Jupyter notebook
os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true"
# This is for setting up django
django.setup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment