Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
2Abstract
3--------
5Projects and apps that implement a REST API often choose `Django REST Framework`_. This package focuses on
6making the registration of REST resources easy; it's usefulness is derived from a simple yet effective system of
7definition and auto-discovery of REST APIs across a project.
9.. _Django REST Framework: https://www.django-rest-framework.org
11Benefits include:
13- Use Django REST Framework as usual, but with auto-discovery of API resources.
14- Consistent view names that use a convention similar to the Django Admin to avoid collisions.
15- Built-in support for new API versions.
17Install
18-------
20Django REST Framework is required:
22``pip install django-rest-framework;``
24Update project settings:
26.. code-block:: python
28 # settings.py
29 INSTALLED_APPS = [
30 "rest_framework",
31 # ...
32 "superdjango.interfaces.reset.apps.AutoConfig",
33 # ...
34 ]
36 SUPERDJANGO_API_MODULE_NAME = "api"
38The ``SUPERDJANGO_API_MODULE_NAME`` may be used to identify the package or module name used to find your API definition.
39It defaults to ``api``, but may be set to anything you like, provided it is consistent across your project.
41Utilize the URLs:
43.. code-block:: python
45 # main/urls.py
46 from rest_framework import routers
47 from superdjango.interfaces.reset import api
49 router = routers.DefaultRouter()
51 urlpatterns = [
52 # ...
53 path('api/v1/', include(api.get_urls(router))),
54 path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
55 # ...
56 ]
58Usage
59-----
61Create serializers and viewsets. See `Django REST Framework Quickstart`_.
63.. _Django REST Framework Quickstart: https://www.django-rest-framework.org/tutorial/quickstart/#serializers
65In the ``__init__.py`` of the app's api module, create and register the interface.
67.. code-block:: python
69 # api/__init__.py
70 from superdjango.interfaces.reset import api, REST
71 from .views import MyViewSet, MyOtherViewSet
73 class MyAPI(REST):
74 viewsets = [
75 MyViewSet,
76 MyOtherViewSet,
77 ]
79 api.register("myapp", MyAPI)
81"""
82from django.conf import settings
83from django.utils.module_loading import autodiscover_modules
84from .interfaces import api
85from .library import REST
87__author__ = "Shawn Davis <shawn@superdjango.com>"
88__maintainer__ = "Shawn Davis <shawn@superdjango.com>"
89__version__ = "0.5.0-x"
91__all__ = (
92 "api",
93 "REST",
94)
96API_MODULE_NAME = getattr(settings, "SUPERDJANGO_API_MODULE_NAME", "api")
99def autodiscover():
100 """Automatically find (and load) ``ui.py`` files located in installed Django apps."""
101 autodiscover_modules(API_MODULE_NAME)
104default_app_config = "superdjango.interfaces.reset.apps.DefaultConfig"