APIView
-
-
Entry function
-
as_view
-
queryset detection
-
Do not directly operate queryset, direct operation between a plurality of data requests disorder occurs
-
Recommended for all or get_queryset
-
-
Call the parent class as_view
-
Parameter detection
-
Defines the closure function
-
Record data
-
dispatch
-
-
-
dispatch
-
Overriding methods
-
Request made a conversion (initialization)
-
After initializing the whole
-
Formatting suffix
-
Content Decision
-
Decision version
-
Mandatory upgrade
-
Recommended upgrade
-
No upgrade
-
-
Perform authentication
-
Check permissions
-
Throttling, limiting
-
-
The method for distribution request
-
If the above process to qualify abnormalities, abnormal and not directly throw, the exception will be caught
-
Provide policy exception
-
The final response unified process
-
Finally, return
generics
-
-
-
APIView
-
-
Attributes
-
filter_backends
-
lookup_field
-
lookup_url_kwarg
-
pagination_class
-
queryset
-
serializer_class
-
-
method
-
get_queryset
-
get_object
-
get_serializer
-
get_serializer_class
-
get_serializer_context
-
filter_queryset
-
paginator
-
Using property modified
-
-
paginate_queryset
-
get_paginated_response
-
-
-
CreateAPIView
-
inherit
-
GenericAPIView
-
CreateModelMixin
-
-
method
-
post
-
create
-
-
-
-
ListAPIView
-
inherit
-
GenericAPIView
-
ListModelMixin
-
-
method
-
get
-
list
-
-
-
-
RetrieveAPIView
-
inherit
-
GenericAPIView
-
RetrieveModelMixin
-
-
method
-
get
-
retrieve
-
-
-
-
DestroyAPIView
-
inherit
-
GenericAPIView
-
DestroyModelMixin
-
-
method
-
delete
-
destroy
-
-
-
-
UpdateAPIView
-
inherit
-
GenericAPIView
-
UpdateModelMixin
-
-
method
-
put
-
update
-
-
patch
-
partial_update
-
-
-
-
ListCreateAPIView
-
inherit
-
GenericAPIView
-
ListModelMixin
-
CreateModelMixin
-
-
method
-
post
-
create
-
-
get
-
list
-
-
-
-
RetrieveUpdateAPIView
-
inherit
-
GenericAPIView
-
RetrieveModelMixin
-
UpdateModelMixin
-
-
method
-
get
-
retrieve
-
-
put
-
update
-
-
patch
-
partial_update
-
-
-
-
RetrieveDestroyAPIView
-
inherit
-
GenericAPIView
-
RetrieveModelMixin
-
DestroyModelMixin
-
-
method
-
get
-
retrieve
-
-
delete
-
destroy
-
-
-
-
RetrieveUpdateDestroyAPIView
-
inherit
-
GenericAPIView
-
RetrieveModelMixin
-
UpdateModelMixin
-
DestroyModelMixin
-
-
method
-
get
-
retrieve
-
-
put
-
update
-
-
patch
-
partial_update
-
-
delete
-
destroy
mixins
-
CreateModelMixin
-
function
-
create
-
perform_create
-
get_success_headers
-
-
-
ListModelMixin (plural)
-
function
-
list
-
-
-
RetrieveModelMixin (单数)
-
function
-
retrieve
-
-
-
UpdateModelMixin (单数)
-
function
-
-
perform_update
-
partial_update
-
-
-
DestroyModelMixin (单数)
-
function
-
destroy
-
perform_destroy
Usage (simple registration login)
- Create a model
class User(models.Model): u_name = models.CharField(max_length=32, unique=True) u_password = models.CharField(max_length=256) @classmethod def get_user(cls, u_name): try: user = User.objects.get(u_name=u_name) return user except Exception as e: print(e) def check_password(self, password): return self.u_password == password
- Generated migration file
- Create a serializer
class UserSerializer(serializers.ModelSerializer): class Meta: model = User fields = ("id", "u_name", "u_password")
- Create a route
urlpatterns = [ url (r ' ^ users / ' , views.UsersAPIView.as_view ()) ]
- Create a view class
class UsersAPIView(CreateAPIView): queryset = User.objects.all() serializer_class = UserSerializer def post(self, request, *args, **kwargs): action = request.query_params.get("action") if action == "register": return self.do_register(request, *args, **kwargs) elif action == "login": return self.do_login(request, *args, **kwargs) else: raise APIException(detail="please supply correct action [register, login]") def do_register(self, request, *args, **kwargs): print(type(request)) print(type(self)) return self.create(request, *args, **kwargs) def do_login(self, request, *args, **kwargs): u_name = request.data.get("u_name") u_password = request.data.get("u_password") user = User.get_user(u_name) if not user: raise APIException(detail="用户不存在") if not user.check_password(u_password): raise APIException(detail="密码错误") token = uuid.uuid4().hex # 存token data = { "msg": "ok", "status": HTTP_200_OK, "token": token } return Response(data)