Created
October 25, 2022 20:05
-
-
Save emoss08/d3544fcbc4ccfc8c1969ac3fbc833a6c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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