94👍
With something like this you’re probably best off breaking out of the generic views and writing the view yourself.
@api_view(['GET'])
def current_user(request):
serializer = UserSerializer(request.user)
return Response(serializer.data)
You could also do the same thing using a class based view like so…
class CurrentUserView(APIView):
def get(self, request):
serializer = UserSerializer(request.user)
return Response(serializer.data)
Of course, there’s also no requirement that you use a serializer, you could equally well just pull out the fields you need from the user instance.
@api_view(['GET'])
def current_user(request):
user = request.user
return Response({
'username': user.username,
'email': user.email,
...
})
38👍
The best way is to use the power of viewsets.ModelViewSet
like so:
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer
def get_object(self):
pk = self.kwargs.get('pk')
if pk == "current":
return self.request.user
return super().get_object()
viewsets.ModelViewSet
is a combination of mixins.CreateModelMixin
+ mixins.RetrieveModelMixin
+ mixins.UpdateModelMixin
+ mixins.DestroyModelMixin
+ mixins.ListModelMixin
+ viewsets.GenericViewSet
. If you need just list all or get particular user including currently authenticated you need just replace it like this
class UserViewSet(mixins.RetrieveModelMixin, mixins.ListModelMixin, viewsets.GenericViewSet):
# ...
- [Django]-How do you get PyPy, Django and PostgreSQL to work together?
- [Django]-Cron and virtualenv
- [Django]-Django/DRF – 405 Method not allowed on DELETE operation
13👍
If you must use the generic view set for some reason, you could do something like this,
class UserViewSet(viewsets.ModelViewSet):
serializer_class = UserSerializer
def get_object(self):
return self.request.user
def list(self, request, *args, **kwargs):
return self.retrieve(request, *args, **kwargs)
retrieve
method is called when the client requests a single instance using an identifier like a primary key /users/10
would trigger the retrieve method normally. Retrieve itself calls get_object
. If you want the view to always return the current used then you could modify get_object
and force list
method to return a single item instead of a list by calling and returning self.retrieve
inside it.
- [Django]-How to remove all of the data in a table using Django
- [Django]-Google Static Maps URL length limit
- [Django]-How do you use the django-filter package with a list of parameters?
13👍
Instead of using full power of ModelViewSet
you can use mixins. There is RetrieveModelMixin
used to retrieve single object just like it is mentioned here – http://www.django-rest-framework.org/api-guide/viewsets/#example_3
class UserViewSet(mixins.RetrieveModelMixin, viewsets.GenericViewSet):
permission_classes = (permissions.IsAuthenticated,)
serializer_class = UserSerializer
def get_object(self):
return self.request.user
If you need also update your model, just add UpdateModelMixin
.
- [Django]-Favorite Django Tips & Features?
- [Django]-Django test runner not finding tests
- [Django]-Django: How to manage development and production settings?
9👍
I used a ModelViewSet
like this:
class UserViewSet(viewsets.ModelViewSet):
serializer_class = UserSerializer
def dispatch(self, request, *args, **kwargs):
if kwargs.get('pk') == 'current' and request.user:
kwargs['pk'] = request.user.pk
return super().dispatch(request, *args, **kwargs)
- [Django]-Django SUM Query?
- [Django]-What's the purpose of Django setting ‘SECRET_KEY’?
- [Django]-Select DISTINCT individual columns in django?
2👍
Use this way to get logged in user data in django rest framework
class LoggedInUserView(APIView):
def get(self, request):
serializer = UserSerializer(self.request.user)
return Response(serializer.data)
Add the api in urls.py file.
path('logged_in_user', LoggedInUserView.as_view())
- [Django]-Django Model() vs Model.objects.create()
- [Django]-Django – iterate number in for loop of a template
- [Django]-Django filter JSONField list of dicts