Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Django REST Framework RESTful Django GET Requests with APIViews

Bernardo Augusto García Loaiza
PLUS
Bernardo Augusto García Loaiza
Courses Plus Student 792 Points

What is the difference in use or inherit from APIView and Model ViewSet

Hi, I would meet the difference with respect to when use API view and when use ModelViewSet when I want serialize my models with respect to get a list of them?

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?