Created
March 28, 2012 00:18
-
-
Save k4ml/2221909 to your computer and use it in GitHub Desktop.
Refactor of gist:2219751 to make it reusable
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
""" | |
This use mini_django.py, sort of micro framework based on django. | |
""" | |
import mini_django | |
mini_django.configure() | |
from django.db import models | |
from django.contrib import admin | |
from django.http import HttpResponse | |
class SomeModel(models.Model): | |
field_name = models.CharField(max_length=10) | |
class Meta: | |
app_label = mini_django.this_module(__file__) | |
__module__ = mini_django.this_module(__file__) | |
try: | |
admin.site.register(SomeModel) | |
except admin.sites.AlreadyRegistered: | |
pass | |
admin.autodiscover() | |
def index(request): | |
out = [] | |
for obj in SomeModel.objects.all(): | |
out.append(obj.field_name) | |
return HttpResponse(','.join(out)) | |
urls = ( | |
(r'^$', index), | |
(r'^admin/', mini_django.include(admin.site.urls)), | |
) | |
if __name__ == '__main__': | |
mini_django.run(urls, __file__, __name__) |
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
""" | |
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] | |
this_module = lambda thisfile: osp.splitext(osp.basename(thisfile))[0] | |
def configure(**new_settings): | |
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', | |
) | |
SETTINGS.update(**new_settings) | |
if not settings.configured: | |
settings.configure(**SETTINGS) | |
from django.conf.urls.defaults import patterns, url, include | |
urlpatterns = None | |
def run(urls, this_file, module_name, settings=None): | |
from django.db import models | |
global urlpatterns | |
urlpatterns = patterns('', *urls) | |
this = this_module(this_file) | |
# override get_app to work with us | |
get_app_orig = models.get_app | |
def get_app(app_label,*a, **kw): | |
if app_label == this_module(this_file): | |
return sys.modules[module_name] | |
return get_app_orig(app_label, *a, **kw) | |
models.get_app = get_app | |
models.loading.cache.app_store[type(this+'.models',(),{'__file__': this_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