[Answer]-Why can't I objects.get() a Django User by pk or id?

0๐Ÿ‘

โœ…

tgdn helped me realize that I was doing something really dumb: I was trying to filter down to a specific user in get_queryset() when I should have been returning all users.

I was able to get things working like this:

# Update a User
class DisableCompanyUserView(UpdateAPIView):
    model = User
    serializer_class = UserSerializer
    lookup_url_kwarg = 'user_id'

    def get_queryset(self):
        return User.objects.all()

    def partial_update(self, request, *args, **kwargs):
        kwargs['partial'] = True
        return self.update(request, *args, **kwargs)

Which I simplified down to this:

# Update a User
class DisableCompanyUserView(UpdateAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer
    lookup_url_kwarg = 'user_id'

    def partial_update(self, request, *args, **kwargs):
        kwargs['partial'] = True
        return self.update(request, *args, **kwargs)
๐Ÿ‘คIAmKale

1๐Ÿ‘

And if you try looking it up with

User.objects.get(id=theId)

Does that return any object?

pk is only a short form of id__iexact

EDIT

Have you tried manually doing

User.objects.filter(pk=yourID)[0]

This should get you results. Otherwise, try

User.objects.all()

And look if you can find the user you are looking for

Leave a comment