Skip to content

Instantly share code, notes, and snippets.

@olivergeorge
Last active August 29, 2015 14:03
Show Gist options
  • Save olivergeorge/32331359a66e01802b23 to your computer and use it in GitHub Desktop.
Save olivergeorge/32331359a66e01802b23 to your computer and use it in GitHub Desktop.
Does this look like a reasonable way to bootstrap a view with details of a local Django REST Framework api? My goal is to avoid my app from having to fetch the root and options before it can start rendering...
from api.urls import router
from django.core.serializers import serialize
from django.core.serializers.json import DjangoJSONEncoder
from django.utils import simplejson
from django.utils.safestring import mark_safe
def test_ui(request):
"Simple view to bootstrap the ui based on the rest api"
endpoints = router.get_api_root_view()(request).data
names = endpoints.keys()
data = {}
for (prefix, viewset, basename) in router.registry:
if prefix in names:
data[prefix] = dict(url=endpoints[prefix], options=viewset().options(request).data)
api = mark_safe(simplejson.dumps(data, cls=DjangoJSONEncoder))
return render(request, 'ui.html', dict(api=api))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment