Skip to content

Instantly share code, notes, and snippets.

@emoss08
Created October 25, 2022 20:05
Show Gist options
  • Save emoss08/d3544fcbc4ccfc8c1969ac3fbc833a6c to your computer and use it in GitHub Desktop.
Save emoss08/d3544fcbc4ccfc8c1969ac3fbc833a6c to your computer and use it in GitHub Desktop.
class ClogGenericAPIView(viewsets.ViewSet):
"""
Generic API View for CLOG use this class for any redundant logic.
"""
model = None
serializer = None
def list(self, request):
queryset = self.model.objects.all() # Or you could do a selector instead of model
serializer = self.serializer(queryset, many=True)
return Response(serializer.data)
def retrieve(self, request, pk=None):
queryset = self.model.objects.all() # Or you could do a selector instead of model
record = get_object_or_404(queryset, pk=pk)
serializer = self.serializer(record)
return Response(serializer.data)
# Rest of the methods go here
# api.py
class UserViewSet(ClogGenericAPIView):
model = User
serializer = UserSerializer
# No need to write any of the methods because the inherited class has them all
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment