By default RetrieveAPIView or RetrieveUpdateAPIView requires lookup_field
to retrieve Model.
However in my case, I want to retrieve my model by self.request.user.
Here is views.py example
class ProfileRetrieveAndUpdateProfile(generics.RetrieveUpdateAPIView):
queryset = Profile.objects.all()
serializer_class = ProfileRetrieveAndUpdateSerializer
lookup_field = 'user_id'
def get_queryset(self):
qs = Profile.objects.all()
logged_in_user_profile = qs.filter(user=self.request.user)
return logged_in_user_profile
Can I use RetrieveAPIView without lookup_field?
You need override
get_object