Last active
July 5, 2016 17:16
-
-
Save donkirkby/2f413797c9e933f3550c to your computer and use it in GitHub Desktop.
𝜇Django - a minimal, but runnable, Django snippet
This file contains 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
# Tested with Django 1.9.2 | |
import sys | |
import django | |
from django.apps import apps | |
from django.apps.config import AppConfig | |
from django.conf import settings | |
from django.db import connections, models, DEFAULT_DB_ALIAS | |
from django.db.models.base import ModelBase | |
NAME = 'udjango' | |
def main(): | |
setup() | |
class Person(models.Model): | |
first_name = models.CharField(max_length=30) | |
last_name = models.CharField(max_length=30) | |
syncdb(Person) | |
p1 = Person(first_name='Jimmy', last_name='Jones') | |
p1.save() | |
p2 = Person(first_name='Bob', last_name='Brown') | |
p2.save() | |
print ', '.join([p.first_name for p in Person.objects.all()]) | |
def setup(): | |
DB_FILE = NAME + '.db' | |
with open(DB_FILE, 'w'): | |
pass # wipe the database | |
settings.configure( | |
DEBUG=True, | |
DATABASES={ | |
DEFAULT_DB_ALIAS: { | |
'ENGINE': 'django.db.backends.sqlite3', | |
'NAME': DB_FILE}}, | |
LOGGING={'version': 1, | |
'disable_existing_loggers': False, | |
'formatters': { | |
'debug': { | |
'format': '%(asctime)s[%(levelname)s]' | |
'%(name)s.%(funcName)s(): %(message)s', | |
'datefmt': '%Y-%m-%d %H:%M:%S'}}, | |
'handlers': { | |
'console': { | |
'level': 'DEBUG', | |
'class': 'logging.StreamHandler', | |
'formatter': 'debug'}}, | |
'root': { | |
'handlers': ['console'], | |
'level': 'WARN'}, | |
'loggers': { | |
"django.db": {"level": "WARN"}}}) | |
app_config = AppConfig(NAME, sys.modules['__main__']) | |
apps.populate([app_config]) | |
django.setup() | |
original_new_func = ModelBase.__new__ | |
@staticmethod | |
def patched_new(cls, name, bases, attrs): | |
if 'Meta' not in attrs: | |
class Meta: | |
app_label = NAME | |
attrs['Meta'] = Meta | |
return original_new_func(cls, name, bases, attrs) | |
ModelBase.__new__ = patched_new | |
def syncdb(model): | |
""" Standard syncdb expects models to be in reliable locations. | |
Based on https://github.com/django/django/blob/1.9.3 | |
/django/core/management/commands/migrate.py#L285 | |
""" | |
connection = connections[DEFAULT_DB_ALIAS] | |
with connection.schema_editor() as editor: | |
editor.create_model(model) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My goal was to create a runnable Django snippet that included a model. This code is also posted in a full repository.
I found several useful resources, but most no longer work under Django 1.7: