Last active
December 7, 2021 11:28
-
-
Save sprytnyk/8f11ada7a6a358ea7abebe301708fcc5 to your computer and use it in GitHub Desktop.
An example of Django standalone script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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