Skip to content

@k4ml /mini_django.py
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Django in single file with model and admin
"""
Django in single file with model and admin. Based on:-
http://fahhem.com/blog/2011/10/django-models-without-apps-or-everything-django-truly-in-a-single-file/
"""
import sys
from os import path as osp
def rel_path(*p): return osp.normpath(osp.join(rel_path.path, *p))
rel_path.path = osp.abspath(osp.dirname(__file__))
this = osp.splitext(osp.basename(__file__))[0]
from django.conf import settings
SETTINGS = dict(
SITE_ID=1,
DATABASES = {},
DEBUG=True,
TEMPLATE_DEBUG=True,
ROOT_URLCONF = this
)
SETTINGS['TEMPLATE_DIRS'] = (rel_path(),),
SETTINGS['DATABASES']={
'default':{
'ENGINE':'django.db.backends.sqlite3',
'NAME':rel_path('db')
}
}
SETTINGS['INSTALLED_APPS'] = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
#'django.contrib.staticfiles',
'django.contrib.admin',
)
if not settings.configured:
settings.configure(**SETTINGS)
from django.db import models
from django.contrib import admin
class SomeModel(models.Model):
field_name = models.CharField(max_length=10)
class Meta:
app_label = this
__module__ = this
try:
admin.site.register(SomeModel)
except admin.sites.AlreadyRegistered:
pass
admin.autodiscover()
from django.conf.urls.defaults import patterns, url, include
from django.http import HttpResponse
def view_name(request):
out = []
for obj in SomeModel.objects.all():
out.append(obj.field_name)
return HttpResponse(','.join(out))
urlpatterns = patterns('',
(r'^$', view_name),
url(r'^admin/', include(admin.site.urls)),
)
if __name__=='__main__':
# override get_app to work with us
get_app_orig = models.get_app
def get_app(app_label,*a, **kw):
if app_label==this:
return sys.modules[__name__]
return get_app_orig(app_label, *a, **kw)
models.get_app = get_app
models.loading.cache.app_store[type(this+'.models',(),{'__file__':__file__})] = this
from django.core import management
management.execute_from_command_line()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.