2013-07-05 21 views
11

Trong django RestFramework, có cách nào "chính thức" để tạo tài liệu cho "Api Root" không?Trong Django RestFramework, làm thế nào để thay đổi tài liệu Api Root?

Sau khi xem xét mã nguồn của RestFramework, tôi đã tìm thấy một công việc xung quanh bằng cách subclassing DefaultRouter:

from rest_framework import routers 

class MyRouter(routers.DefaultRouter): 
    def get_api_root_view(self): 
     api_root_view = super(MyRouter, self).get_api_root_view() 
     ApiRootClass = api_root_view.cls 

     class MyAPIRoot(ApiRootClass): 
      """My API Root documentation""" 
      pass 

     return MyAPIRoot.as_view() 

router = MyRouter() 

Có một trình dọn dẹp hay cách nào tốt hơn?

Trả lời

7

Tôi mới làm điều này nhưng tôi thấy bạn có thể sử dụng SimpleRouter thay vì DefaultRouter để chỉ định APIRoot của riêng bạn.

trong urls.py trong mô-đun api của bạn

from django.conf.urls import patterns, url, include 
from rest_framework.routers import SimpleRouter 
router = SimpleRouter() 

urlpatterns = patterns('api.views', 
    url(r'^$', views.APIRoot.as_view()), 
    url(r'', include(router.urls)), 
) 

Sau đó xác định các tài liệu trong các bình luận lớp

from rest_framework import generics 

class APIRoot(generics.GenericAPIView): 
    """ 
    My API documentation 
    """ 

+0

với cách tiếp cận của bạn, sử dụng 'SimpleRouter' hoặc' DefaultRouter' không thực sự quan trọng. Bạn vẫn có thể sử dụng 'DefaultRouter' mà không gặp bất kỳ vấn đề gì. Đặc biệt nếu bạn cần [** định dạng hậu tố **] (http://www.django-rest-framework.org/api-guide/routers/#defaultrouter) được cung cấp từ nó. – Yeo

+0

Điều gì sẽ là cách tốt nhất để làm điều tương tự, nhưng không phá vỡ bất cứ điều gì khác? Tôi có nghĩa là điều này sẽ ghi đè hoàn toàn chế độ xem gốc API bình thường, vì vậy mọi thứ có thể ngừng hoạt động. – Joakim

3

Đó là loại khó khăn để ghi đè lên lớp APIRoot. Cách đơn giản nhất để đạt được những gì bạn muốn có lẽ là để thay đổi thuộc tính của __doc__ APIRootClass khi chạy trong urls.py của bạn:

class Router(routers.DefaultRouter): 
    def get_api_root_view(self, api_urls=None): 
     root_view = super(Router, self).get_api_root_view(api_urls=api_urls) 
     root_view.cls.__doc__ = "Place your documentation here" 
     return root_view 

router = Router() 
router.register(...) 

urlpatterns = [ 
    url(r'^', include(router.urls)), 
] 
+0

Trong Python 2 bạn nhận được một ngoại lệ cố gắng để viết '__doc__' – Joakim

8

Tôi tìm thấy một giải pháp thông qua thử nghiệm.

Tôi thích nó với các giải pháp khác trong chuỗi này vì nó yêu cầu ít mã hơn và cho phép bạn tùy chỉnh tiêu đề API, cũng như tài liệu cho gốc API.

from rest_framework import routers 

class ThisWillBeTheApiTitleView(routers.APIRootView): 
    """ 
    This appears where the docstring goes! 
    """ 
    pass 


class DocumentedRouter(routers.DefaultRouter): 
    APIRootView = ThisWillBeTheApiTitleView 


router = DocumentedRouter() 
router.register(r'items', ItemsViewSet) 

Điều này ám như dưới đây:

Demonstration