向路由器或视图集添加更多视图(Django-Rest-Framework)

时间:2013-09-23 22:15:06

标签: django django-rest-framework

基本上,我正在尝试找到一种将更多视图附加到路由器而不创建自定义路由器的好方法。 实现这一目标的好方法是什么?

这有点类似于我想要完成的事情。变量名已被更改,我想引入的示例方法为了这个问题而极为简化。

路由器:

router = routers.SimpleRouter(trailing_slash=False)
router.register(r'myobjects', MyObjectViewSet, base_name='myobjects')
urlpatterns = router.urls

视图集

class MyObjectsViewSet(viewsets.ViewSet):
""" Provides API Methods to manage MyObjects. """

def list(self, request):
    """ Returns a list of MyObjects. """
    data = get_list_of_myobjects()
    return Response(data)

def retrieve(self, request, pk):
    """ Returns a single MyObject. """
    data = fetch_my_object(pk)
    return Response(data)

def destroy(self, request, pk):
    """ Deletes a single MyObject. """
    fetch_my_object_and_delete(pk)
            return Response()

我需要包含的另一种方法类型的一个示例。 (其中有很多):

def get_locations(self, request):
    """ Returns a list of location objects somehow related to MyObject """
    locations = calculate_something()
    return Response(locations)

最终结果是以下URL可以正常工作并“干净地”实现。

GET example.com/myobjects/123/locations

4 个答案:

答案 0 :(得分:24)

上面的answer given by mariodev是正确的,只要您只想提出GET次请求。

如果你想要POST函数附加到ViewSet,你需要使用action装饰器:

from rest_framework.decorators import action, link
from rest_framework.response import Response

class MyObjectsViewSet(viewsets.ViewSet):

    # For GET Requests
    @link()
    def get_locations(self, request):
        """ Returns a list of location objects somehow related to MyObject """
        locations = calculate_something()
        return Response(locations)

    # For POST Requests
    @action()
    def update_location(self, request, pk):
        """ Updates the object identified by the pk """
        location = self.get_object()
        location.field = update_location_field() # your custom code
        location.save()

        # ...create a serializer and return with updated data...

然后你会POST到一个格式如下的网址: /myobjects/123/update_location/

如果您有兴趣,

http://www.django-rest-framework.org/api-guide/viewsets/#marking-extra-actions-for-routing会提供更多信息!

答案 1 :(得分:14)

现在可以使用list_route和detail_route装饰器执行此操作:http://www.django-rest-framework.org/api-guide/viewsets/#marking-extra-actions-for-routing

例如:

from rest_framework.decorators import list_route
from rest_framework.response import Response
...

class MyObjectsViewSet(viewsets.ViewSet):
    ...

    @list_route()
    def locations(self, request):
        queryset = get_locations()
        serializer = LocationSerializer(queryset, many=True)
        return Response(serializer.data)

答案 2 :(得分:8)

您可以像现在一样定义方法,但是您需要使用相同的url作为方法名称并添加链接装饰器,因此对于

/myobjects/123/locations/

你可以添加像这样的方法

@link(permission_classes=[...])
def locations(self, request, pk=None):
    ...

并且路由器将自动选择它。

答案 3 :(得分:3)

来自Routing to extra methods on a ViewSet

  

我认为您可能需要手动路由该方法,即The Old-Fashioned Way™。

     

首先将该方法作为单独的视图拉出:

   set_password_view = UserViewSet.as_view({'post': 'set_password'})
     

(或类似)

     

然后分配您的网址:

   url(r'^users/username_available/$', set_password_view, name-=...)
     

(或者这样)

related question on SO