The profile views are good candidates to reuse some common patterns with Generic Views from Django REST framework and make the code more DRY.
ProfileRetrieveAPIView looks like this:
class ProfileRetrieveAPIView(RetrieveAPIView):
permission_classes = (AllowAny,)
queryset = Profile.objects.select_related('user')
renderer_classes = (ProfileJSONRenderer,)
serializer_class = ProfileSerializer
def retrieve(self, request, username, *args, **kwargs):
# Try to retrieve the requested profile and throw an exception if the
# profile could not be found.
try:
profile = self.queryset.get(user__username=username)
except Profile.DoesNotExist:
raise NotFound('A profile with this username does not exist.')
serializer = self.serializer_class(profile, context={
'request': request
})
return Response(serializer.data, status=status.HTTP_200_OK)
Using generic views features, will look like this:
The profile views are good candidates to reuse some common patterns with Generic Views from Django REST framework and make the code more DRY.
ProfileRetrieveAPIView
looks like this:Using generic views features, will look like this: