Hide keyboard shortcuts

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-------- 

4 

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. 

8 

9.. _Django REST Framework: https://www.django-rest-framework.org 

10 

11Benefits include: 

12 

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. 

16 

17Install 

18------- 

19 

20Django REST Framework is required: 

21 

22``pip install django-rest-framework;`` 

23 

24Update project settings: 

25 

26.. code-block:: python 

27 

28 # settings.py 

29 INSTALLED_APPS = [ 

30 "rest_framework", 

31 # ... 

32 "superdjango.interfaces.reset.apps.AutoConfig", 

33 # ... 

34 ] 

35 

36 SUPERDJANGO_API_MODULE_NAME = "api" 

37 

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. 

40 

41Utilize the URLs: 

42 

43.. code-block:: python 

44 

45 # main/urls.py 

46 from rest_framework import routers 

47 from superdjango.interfaces.reset import api 

48 

49 router = routers.DefaultRouter() 

50 

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 ] 

57 

58Usage 

59----- 

60 

61Create serializers and viewsets. See `Django REST Framework Quickstart`_. 

62 

63.. _Django REST Framework Quickstart: https://www.django-rest-framework.org/tutorial/quickstart/#serializers 

64 

65In the ``__init__.py`` of the app's api module, create and register the interface. 

66 

67.. code-block:: python 

68 

69 # api/__init__.py 

70 from superdjango.interfaces.reset import api, REST 

71 from .views import MyViewSet, MyOtherViewSet 

72 

73 class MyAPI(REST): 

74 viewsets = [ 

75 MyViewSet, 

76 MyOtherViewSet, 

77 ] 

78 

79 api.register("myapp", MyAPI) 

80 

81""" 

82from django.conf import settings 

83from django.utils.module_loading import autodiscover_modules 

84from .interfaces import api 

85from .library import REST 

86 

87__author__ = "Shawn Davis <shawn@superdjango.com>" 

88__maintainer__ = "Shawn Davis <shawn@superdjango.com>" 

89__version__ = "0.5.0-x" 

90 

91__all__ = ( 

92 "api", 

93 "REST", 

94) 

95 

96API_MODULE_NAME = getattr(settings, "SUPERDJANGO_API_MODULE_NAME", "api") 

97 

98 

99def autodiscover(): 

100 """Automatically find (and load) ``ui.py`` files located in installed Django apps.""" 

101 autodiscover_modules(API_MODULE_NAME) 

102 

103 

104default_app_config = "superdjango.interfaces.reset.apps.DefaultConfig"