Skip to content

Commit

Permalink
refactor: use custom viewset base
Browse files Browse the repository at this point in the history
create custom viewset `UpdateRetrieveDestroyViewSet` base for reuse
  • Loading branch information
moonlitgrace committed Dec 14, 2024
1 parent 447011d commit 28910b0
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
1 change: 0 additions & 1 deletion backend/apps/comment/api/v1/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,5 @@ def create(self, validated_data):
comment_instance: CommentModel = CommentModel.objects.create_child(
**data
) # pyright: ignore

comment_instance.save()
return comment_instance
4 changes: 2 additions & 2 deletions backend/apps/comment/api/v1/viewsets.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from rest_framework import viewsets
from common.api.bases.viewsets import UpdateRetrieveDestroyViewSet

from ...models import CommentModel
from .serializers import CommentSerializer


class CommentModelViewSet(viewsets.ModelViewSet):
class CommentModelViewSet(UpdateRetrieveDestroyViewSet):
queryset = CommentModel.objects.all()
serializer_class = CommentSerializer

Expand Down
Empty file.
17 changes: 17 additions & 0 deletions backend/common/api/bases/viewsets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from rest_framework import mixins, viewsets


class UpdateRetrieveDestroyViewSet(
mixins.UpdateModelMixin,
mixins.RetrieveModelMixin,
mixins.DestroyModelMixin,
viewsets.GenericViewSet,
):
"""
A viewset that provides `update`, `retrieve`, and `destroy` actions.
To use it, override the class and set the `.queryset` and
`.serializer_class` attributes.
"""

pass

0 comments on commit 28910b0

Please sign in to comment.