I would meet the difference with respect to when use APIView and when use ModelViewSet when I want serialize my models with respect to get a list of their objects/records?
For example, in the APIView documentation we have that with the ListUser class and their get method we can get the list of users
class ListUsers(APIView):
"""
View to list all users in the system.
* Requires token authentication.
* Only admin users are able to access this view.
"""
authentication_classes = (authentication.TokenAuthentication,)
permission_classes = (permissions.IsAdminUser,)
def get(self, request, format=None):
"""
Return a list of all users.
"""
usernames = [user.username for user in User.objects.all()]
return Response(usernames)
I have been get this same list of users using ModelViewSet of this way:
class UserViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows users to be viewed or edited.
"""
queryset = User.objects.all().order_by('-date_joined')
serializer_class = UserSerializer
filter_fields = ('username', 'is_player', 'first_name', 'last_name', 'team' , 'email', )
How to can I identify when should I use APIView or ModelViewSet for this task?