Commit fea35c30 authored by Ashish's avatar Ashish
Browse files


parent 81696be1
API Builder
Build dynamic API based on the provided SQLAlchemy model
from viewsets import AlchemyModelViewSet
from rest_framework_nested import routers
from managers import AlchemyModelManager
class APIModelBuilder(object):
def __init__(self, models, SessionMixin, *args, **kwargs):
self.models = models
self.SessionMixin = SessionMixin
def urls(self):
router = routers.SimpleRouter()
for model in self.models:
manager = type(
(self.SessionMixin, AlchemyModelManager,),
'model_class': model,
viewset = type(
'manager_class': manager,
router.register(model.__name__.lower() + 's', viewset,
return router.urls
import unittest
from djangorest_alchemy.apibuilder import APIModelBuilder
class TestAPIBuilder(unittest.TestCase):
def test_urls(self):
class Model(object):
class Model2(object):
builder = APIModelBuilder([Model, Model2])
print builder.urls
\ No newline at end of file
......@@ -3,21 +3,27 @@ Url registrations
from django.conf.urls import patterns, include, url
from rest_framework_nested import routers
from djangorest_alchemy.apibuilder import APIModelBuilder
from viewsets import CarViewSet
from viewsets import PartViewSet
viewset_router = routers.SimpleRouter()
viewset_router.register(r'api/cars', CarViewSet,
from models import Car, Part, SessionMixin
#viewset_router = routers.SimpleRouter()
#viewset_router.register(r'api/cars_foo', CarViewSet,
# base_name='car')
# Register the child model
child_router = routers.NestedSimpleRouter(viewset_router, r'api/cars',
child_router.register("parts", PartViewSet,
#child_router = routers.NestedSimpleRouter(viewset_router, r'api/cars_foo',
# lookup='cars')
#child_router.register("parts", PartViewSet,
# base_name='part')
builder = APIModelBuilder([Car, Part], SessionMixin)
urlpatterns = patterns('',
url(r'^', include(viewset_router.urls)),
url(r'^', include(child_router.urls)),
#url(r'^', include(viewset_router.urls)),
#url(r'^', include(child_router.urls)),
url(r'^', include(builder.urls)),
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment