DjangoRest View介紹

class APIView(View)

屬性 值類型 作用 是否必須
permission_classes tuple 權限控制
方法 作用
check_permissions(self, request) Check if the request should be permitted
check_object_permissions(self, request, obj) Check if the request should be permitted for a given object

class GenericAPIView(views.APIView)

Base class for all other generic views.

屬性 值類型 作用 是否必須
queryset QuerySet get_queryset()使用
serializer_class serializer get_serializer_class()使用
lookup_field string If you want to use object lookups other than pk, set ‘lookup_field’
lookup_url_kwarg string url中變量
filter_backends tuple The filter backend classes to use for queryset filtering
pagination_class class The style to use for queryset pagination
方法 作用
get_queryset(self) Get the list of items for this view. Defaults to using self.queryset
get_object(self) Returns the object the view is displaying. 使用此方法會調用self.check_object_permissions(self.request, obj)
get_serializer(self, *args, **kwargs) Return the serializer instance that should be used for validating and deserializing input, and for serializing output.
filter_queryset(self, queryset) Given a queryset, filter it with whichever filter backend is in use
paginate_queryset(self, queryset) Return a single page of results, or None if pagination is disabled.

備註:

  1. 使用GenericAPIView需要自己定義['get', 'post', 'put', 'patch', 'delete']方法,來處理對應的http請求.
  2. 應該始終使用get_queryset()獲取查詢結果,而不是直接訪問self.queryset,因爲self.queryset只被評估一次作爲緩存,爲所有後續請求使用。
  3. GenericAPIView提供的默認方法,可以根據需要override.直接在class中重寫即可.

具體的View

Concrete view classes that provide method handlers by composing the mixin classes with the base view.

  1. CreateAPIView(mixins.CreateModelMixin, GenericAPIView)

    實現了post方法,直接創建model的實例.

    只需要提供querysetserializer_class,就可以直接使用.

  2. ListAPIView(mixins.ListModelMixin, GenericAPIView)

    實現了get方法,獲取model列表

    只需要提供querysetserializer_class,就可以直接使用.

    需要其他擴展功能需要按需設置GenericAPIView的其他屬性.

  3. RetrieveAPIView(mixins.RetrieveModelMixin, GenericAPIView)

    實現了get方法,獲取model實例.

    只需要提供querysetserializer_class,就可以直接使用.

  4. DestroyAPIView(mixins.DestroyModelMixin, GenericAPIView)

    實現了delete方法,刪除model實例.

    只需要提供queryset,就可以直接使用.

  5. UpdateAPIView(mixins.UpdateModelMixin, GenericAPIView)

    實現了putpatch方法,更新model實例.

    只需要提供querysetserializer_class,就可以直接使用.

  6. ListCreateAPIView(mixins.ListModelMixin, mixins.CreateModelMixin, GenericAPIView)

    ListAPIViewCreateAPIView的組合.

    實現了getpost方法,用於獲取model列表和創建model的實例.

  7. RetrieveUpdateAPIView(mixins.RetrieveModelMixin, mixins.UpdateModelMixin, GenericAPIView)

    RetrieveAPIViewUpdateAPIView的組合.

    實現了getputpatch方法.

  8. RetrieveDestroyAPIView(mixins.RetrieveModelMixin, mixins.DestroyModelMixin, GenericAPIView)

    RetrieveAPIViewDestroyAPIView的組合.

    實現了getdelete方法.

  9. RetrieveUpdateDestroyAPIView(mixins.RetrieveModelMixin, mixins.UpdateModelMixin, mixins.DestroyModelMixin, GenericAPIView)

    RetrieveAPIViewUpdateAPIViewDestroyAPIView的組合.

    實現了getputpatchdelete方法.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章