I am just new to the Django Rest Framework and I want to clearly understand how ListCreateAPIView works.
We just can provide a queryset
, serializer_class
and it will create a read-write endpoint.
I was looking for info on the official doc but didn't find what I want.
Any information will be helpful for me.
CodePudding user response:
the story begins from the get
method so when calling get it will call list
method
this is how list method looks like it will call queryset and make pagination then serialize the data to returned as response
def list(self, request, *args, **kwargs):
queryset = self.filter_queryset(self.get_queryset())
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
return self.get_paginated_response(serializer.data)
serializer = self.get_serializer(queryset, many=True)
return Response(serializer.data)
for more information, you can visit this link https://www.cdrf.co/3.12/rest_framework.generics/ListAPIView.html
CodePudding user response:
ListCreateAPIView
is a generic APIView that allows for GET (list) and POST (create) requests.
You can read the source code and maybe get a better understanding
Basically, ListCreateAPIView
has the method get()
which will call the method list()
in mixins.ListModelMixin
. The list
method will instance the serializer, filter, paginate the queryset and return a response based on the queryset and serializer you have defined in your class.
If you want a deeper understanding I recommend you to read the source code, it can be confusing at first but when you starting using it you will understand it better.