Skip to content

Instantly share code, notes, and snippets.

@ImpostorKeanu
Last active January 23, 2021 18:02
Show Gist options
  • Save ImpostorKeanu/27132c88aa51855389421e408a82017a to your computer and use it in GitHub Desktop.
Save ImpostorKeanu/27132c88aa51855389421e408a82017a to your computer and use it in GitHub Desktop.
Use Django's ORM without the web framework
#!/usr/bin/env python3
from django.conf import settings
'''
Useful in situations when you want to use Django's ORM
instead of SQLAlchemy, usually just due to preference.
'''
# ========================
# IMPLEMENT CONFIGURATIONS
# ========================
# IMPORTANT #
'''
- Application directory must match this name
- Directory must be in sys.path, too
- Method of getting the root directory if calling from another module:
- Migrations will be stored in that directory, too
```python3
from pathlib import Path
import sys
sys.path.append(str(Path(__file__).parent.absolute())
```
'''
INSTALLED_APPS = ['app']
# More complex database configurations can be applied, too
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases
DATABASES = {
'default': '/path/to/database/file'
}
# Apply the configurations
settings.configure(
INSTALLED_APPS=INSTALLED_APPS,
DATABASES=DATABASES
)
# Generating/Applying Migrations -or- dropping to a shell
'''
from django.core.management import execute_from_command_line
execute_from_command_line([base_dir,'makemigrations'])
execute_from_command_line([base_dir,'migrate'])
execute_from_command_line([base_dir,'shell'])
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment