Skip to content

Instantly share code, notes, and snippets.

@britonad
Last active December 7, 2021 11:28
Show Gist options
  • Save britonad/8f11ada7a6a358ea7abebe301708fcc5 to your computer and use it in GitHub Desktop.
Save britonad/8f11ada7a6a358ea7abebe301708fcc5 to your computer and use it in GitHub Desktop.
An example of Django standalone script
#!/usr/bin/env python3
import os
import pathlib
import sys
import django
# Set a project root to a const, this file should be placed in a project root
# as well
PROJECT_ROOT = pathlib.Path(__file__).resolve().parent
# Add the project root to sys paths
sys.path.append(PROJECT_ROOT)
# If Django settings module isn't passed through an environement variable we
# set it manually
if not os.getenv('DJANGO_SETTINGS_MODULE'):
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings')
# Setup Django based on the above settings
django.setup()
# Standalone script nightmarish logic goes here
# from app.models import YourModel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment