django-rest-framework: independent GET and PUT in same URL but different generics view

て烟熏妆下的殇ゞ 提交于 2020-01-02 00:57:11

问题


I'm using django-rest-framework and I need to map in the URL file two generic views with the same url (iḿ already using URLs but not Routes):

I need to allow GET, PUT and DELETE verbs in just one url (like /api/places/222) and allow everyone to get every field with the related Entity Place but just allow to update (PUT) one field using the same url.

Place Entity:

- id (not required in PUT)
- name (required always)
- date (not required in PUT but required in POST)

URL

url(r'^api/places/(?P<pk>\d+)/?$', PlacesDetail.as_view(), name='places-detail'),

I tried to use RetrieveDestroyAPIView and UpdateAPIView, but I'm unable to use just one URL.


回答1:


I suggest you to create a few serializers that satisfy your needs. Then override the get_serializer method of your view so that the view switches serializers according to an HTTP request method.

This is a quick untested example:

class PlacesDetail(RetrieveUpdateDestroyAPIView):

    def get_serializer_class(self):
        if self.request.method == 'POST':
            serializer_class = FirstSerializer
        elif self.request.method == 'PUT':
            serializer_class = SecondSerializer

        return serializer_class
    ...

Look at the base class method's comment:

def get_serializer_class(self):
    """
    Return the class to use for the serializer.
    Defaults to using `self.serializer_class`.

    You may want to override this if you need to provide different
    serializations depending on the incoming request.

    (Eg. admins get full serialization, others get basic serialization)
    """
    ...


来源:https://stackoverflow.com/questions/22104487/django-rest-framework-independent-get-and-put-in-same-url-but-different-generic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!