Skip to content

Instantly share code, notes, and snippets.

@mentix02
Last active August 28, 2020 07:38
Show Gist options
  • Save mentix02/d04df51a8ff9b88d3f5d7f99556f5962 to your computer and use it in GitHub Desktop.
Save mentix02/d04df51a8ff9b88d3f5d7f99556f5962 to your computer and use it in GitHub Desktop.
class UserDetailAPIView(RetrieveAPIView):
lookup_url_kwarg = 'username'
queryset = User.objects.all()
lookup_field = 'username__iexact'
serializer_class = UserSerializer
class UserPostListAPIView(ListAPIView):
serializer_class = PostSerializer
permission_classes = (IsAuthenticated,)
def get_queryset(self) -> QuerySet:
username = self.kwargs['username']
user = get_object_or_404(User, username__iexact=username)
if user in self.request.user.following or not user.private:
return Post.objects.filter(user__username__iexact=username)
else:
raise PermissionDenied('Follow user to see their posts.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment