DjangoRest View介绍

class APIView(View)

属性值类型作用是否必须
permission_classestuple权限控制
方法作用
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.

属性值类型作用是否必须
querysetQuerySetget_queryset()使用
serializer_classserializerget_serializer_class()使用
lookup_fieldstringIf you want to use object lookups other than pk, set ‘lookup_field’
lookup_url_kwargstringurl中变量
filter_backendstupleThe filter backend classes to use for queryset filtering
pagination_classclassThe 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方法.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值