-
Notifications
You must be signed in to change notification settings - Fork 1
Tutorial on How to Implement A Django Rest API ViewSet
Mücahit Erdoğan Ünlü edited this page May 15, 2024
·
1 revision
- APIView
- Viewset (ModelViewSet etc)
- You implement every endpoint from scratch. Example:
from rest_framework.views import APIView
from rest_framework.response import Response
class MyView(APIView):
def get(self, request):
# Handle GET request
return Response({'message': 'Hello, world!'})
- Provides default implementations for common CRUD operations, which can be customized by overriding specific methods. Example:
- Needs a serializer for actions (actions are defined below, CRUD-like). The actions act on this defined serializer
- ModelViewSet is a viewset that provides default
create()
,retrieve()
,update()
,partial_update()
,destroy()
andlist()
actions. You can overwrite them. For different actions, you can define different permissions.
# overwrite an action
def list(self, request):
queryset = User.objects.all()
serializer = UserSerializer(queryset, many=True)
return Response(serializer.data)
# self is a viewset object. set a specific permission
if self.action == 'list':
permission_classes = [IsAuthenticated]
- You can create an action that does not inherit any default implementation.
# a new action
@action(detail=False, methods=['GET'], name='Get comments')
def list_comments(self, request, *args, **kwargs):
queryset = Post.objects.filter(in_reply_to_post = self.kwargs["pk"])
serializer = self.get_serializer(queryset)
return Response(serializer.data)
- You have to register your viewset in the URL
from myapp.views import UserViewSet
from rest_framework.routers import DefaultRouter
router = DefaultRouter()
router.register(r'users', UserViewSet, basename='user')
urlpatterns = router.urls
from rest_framework.viewsets import ModelViewSet
from myapp.models import MyModel
from myapp.serializers import MyModelSerializer
class MyModelViewSet(ModelViewSet):
queryset = MyModel.objects.all()
serializer_class = MyModelSerializer
Turquiz App
DONE
- Lab Report #1
- Lab Report #2
- Lab Report #3
- Lab Report #4
- Lab Report #5
- Lab Report #6
- Lab Report #7
- Lab Report for Lab 9
- Lab Meeting #1
- Meeting #1
- Lab Meeting #2
- Meeting #2
- Project Plan Meeting
- Frontend Meeting #1
- Lab Meeting #3
- Meeting #3
- Meeting #4
- Lab Meeting #4
- Meeting #5
- Meeting #6
- Meeting #7
- Meeting #8
- User Scenario 1 - Explore and Register
- User Scenario 2 - Search for a Quiz & Create a Quiz
- User Scenario 3 - Take & Review a Quiz
- User Scenario 4 - Search Forum & Bookmark & Upvote & Answer
- User Scenario 5 - User Badges
- User Scenario Template
- User Stories