4๐
#myapp/views.py
class UserIdViewSet(viewsets.ModelViewSet):
serializer_class = UserSerializer
def get_queryset(self):
return User.objects.filter(id=self.request.user.id)
#myapp/urls.py
router.register(r'api/user-id', userviews.UserIdViewSet, base_name="UserId")
sort out the problem. Basically created View set and sort this out against current user.
๐คRobert
1๐
What type of authentication you use ?
If for example, you use TokenAuthentication from rest_framework, you can have a look how this class implements request authentication.
You can find there methods authenticate
and authenticate_credentials
and I believe that there โ you will find your answer how to get the user.
- [Django]-Pytest-cov cover many applications at once
- [Django]-How to display the label from models.TextChoices in the template?
- [Django]-Locating file path from a <InMemoryUploadedFile> Django object
- [Django]-Django โ AttributeError: module 'os' has no attribute 'environment'
- [Django]-Error when sending email through Sendgrid API
0๐
In the perform_create method you can assign the user to your model
class EmailViewSet(viewsets.ModelViewSet):
authentication_classes = (TokenAuthentication)
permission_classes = (IsAuthenticated,)
queryset = Email.objects.all()
serializer_class = EmailSerializer
def perform_create(self, serializer):
serializer.save(user=self.request.user)
- [Django]-Any clue on this error with generic relation using Django Orm?
- [Django]-How to override the update action in django rest framework ModelViewSet?
- [Django]-Manager isn't accessible via Blog instances
- [Django]-Django import client
Source:stackexchange.com