20👍
The difference is what methods they offer.
For example:
GenericViewSet inherits from GenericAPIView but does not provide any implementations of basic actions. Just only get_object, get_queryset.
ModelViewSet inherits from GenericAPIView and includes implementations for various actions. In other words you dont need implement basic actions as list, retrieve, create, update or destroy. Of course you can override them and implement your own list or your own create methods.
You can read more about it, in the API REFERENCE section:
ModelViewSet
10👍
The difference between Generics or ModelViewSet is that:
-
Convenient
Usually ModelViewSet is more convenient. Because ModelViewSet support creating url pattern automatically with DRF router. But Generics don’t. you do yourself. -
Shorten and crispy code
If you want to create CRUD, Generics needs two classes(ListCreateAPIView and RetrieveUpdateDestroyAPIView). But ModelViewSet needs only one class(ModelViewSet).
Check out Declaration below. Both inherits from GenericAPIView and mixins.CreateModelMixin, mixins.ListModelMixin It provides equivalent functionality basically. It depends on what you prefer. But I usually use ViewSet in most cases.
Declaration
# Generics __________________________________
class ListCreateAPIView(mixins.ListModelMixin,
mixins.CreateModelMixin,
GenericAPIView):
# ModelViewSet _____________________________
class ModelViewSet(mixins.CreateModelMixin,
mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
mixins.DestroyModelMixin,
mixins.ListModelMixin,
GenericViewSet):
# GenericViewSet _____________________________
class GenericViewSet(ViewSetMixin, generics.GenericAPIView):
Example code
# Generics __________________________________
from rest_framework import generics
class BookList(generics.ListCreateAPIView):
queryset = Book.objects.all()
serializer_class = BookSerializer
# ModelViewSet _____________________________
from rest_framework import viewsets
class BookViewSet(viewsets.ModelViewSet):
queryset = Book.objects.all()
serializer_class = BookSerializer
- How to serve static files to AWS when deploying Django app (`python manage.py collectstatic` didn't work)?
- How do I install Mezzanine as a Django app?
7👍
Why there is generics if ModelViewSet gives same abilities and more?
Let me first rephrase the question a little more explicitly for you…
“Why are there Generic Views, when there are also Generic ViewSets”
Which really just comes down to a question of why REST framework supports both views and viewsets. Answer – ViewSets are useful for prototyping or for cases when your API URLs neatly map to a fixed convention throughout (eg CRUD style APIs). Views are useful for being explicit, or for cases when your URLs do not neatly map to a fixed convention throughout.
- How to prevent user to access login page in django when already logged in?
- "unexpected indent" error when using notepad++ for creating django function
- Customizing admin of django to have dependent select fields
0👍
ModelViewSet works with router so you don’t have to map the URLs youself. While with GenericAPIView, you have to define individual URLs for every http method.