{"action":"closed","issue":{"url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/2370","labels_url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/2370/labels{/name}","comments_url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/2370/comments","events_url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/2370/events","html_url":"https://github.com/tomchristie/django-rest-framework/issues/2370","id":53232627,"number":2370,"title":"Reusable apps with APIs","user":{"login":"rakanalh","id":195829,"avatar_url":"https://avatars.githubusercontent.com/u/195829?v=3","gravatar_id":"","url":"https://api.github.com/users/rakanalh","html_url":"https://github.com/rakanalh","followers_url":"https://api.github.com/users/rakanalh/followers","following_url":"https://api.github.com/users/rakanalh/following{/other_user}","gists_url":"https://api.github.com/users/rakanalh/gists{/gist_id}","starred_url":"https://api.github.com/users/rakanalh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rakanalh/subscriptions","organizations_url":"https://api.github.com/users/rakanalh/orgs","repos_url":"https://api.github.com/users/rakanalh/repos","events_url":"https://api.github.com/users/rakanalh/events{/privacy}","received_events_url":"https://api.github.com/users/rakanalh/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-01-02T00:22:47Z","updated_at":"2015-01-02T10:39:17Z","closed_at":"2015-01-02T10:39:17Z","body":"Hello,\r\n\r\nI've been using DRF for a while now and the way i have been using it in my project is by creating an \"api\" django app which basically constructed the URLs, serializers and views for all my other apps.\r\n\r\nRecently i've been wondering about how we could create reusable apps that also ship with their own APIs all wrapped in the same app folder, while also having all api-enabled apps to be under the same URL prefix \"/api/xyz\". Thinking about how this would be achieved, i figured the way to go for this is to actually create a \"url autoloader\".\r\n\r\nThe app is structured as follows:\r\n-> My App\r\n---> models.py\r\n---> admin.py \r\n---> ...etc\r\n---> rest_api\r\n-------> urls.py\r\n-------> serializers.py\r\n-------> views.py\r\n\r\nThe following piece of code would then look through the INSTALLED_APPS and register \"appX.rest_api.urls\" into the urlpatterns.\r\n    \r\n    # Register a generic url autoload according to scheme appx.rest_api.urls\r\n    urlpatterns += autoload_api_urls()\r\n    # Register custom modules which have urls.py within. Those do not follow the above pattern\r\n    urlpatterns += autoload_submodule('api.v1.comments')\r\n    urlpatterns += autoload_submodule('api.v1.pushy')\r\n\r\nThis is the autoloader:\r\n    from django.conf import settings\r\n    from django.conf.urls import patterns, url, include\r\n    from django.utils.importlib import import_module\r\n    from django.utils.module_loading import module_has_submodule\r\n\r\n\r\n    def autoload_api_urls():\r\n        urlpatterns = patterns('')\r\n        for app in settings.INSTALLED_APPS:\r\n            module = import_module(app)\r\n            if not module_has_submodule(module, 'rest_api'):\r\n                continue\r\n\r\n            urlpatterns += autoload_submodule('{}.{}'.format(app, 'rest_api'))\r\n\r\n        return urlpatterns\r\n\r\n\r\n    def autoload_submodule(module_name):\r\n        submodule = import_module(module_name)\r\n        if not module_has_submodule(submodule, 'urls'):\r\n            return []\r\n\r\n        return patterns('', url('', include('{}.{}'.format(module_name, 'urls'))))\r\n\r\nThis is currently working fine on the project i am working on. I would like to publish this as a package but i think it's too small to be one. Should be something as a framework utility so thought you can have an opinion about it. \r\nBasically the motivation behind this is to have one django app to contain everything including it's own APIs and serializers. Rather than having a single app that gathers api views and serializers from all apps. So it's more of a utility for an architectural decision for the app creator more than being just an autoloader implementation.\r\n\r\nYour feedback would be much appreciated on this."}}