I have this viewset
class OrderItemViewSet(viewsets.ModelViewSet):
serializer_class = OrderItemSerializer
def get_queryset(self):
print('Current User', self.request.user, self.action)
return OrderItem.objects.filter(order__owner=self.request.user.profile)
take note of the print('Current User', self.request.user)
I have used that to identify the root of the problem.
urls.py
router.register('order_items', shopping_api.OrderItemViewSet, 'order_items')
So far so good... But when I make a PUT request;
const response = await fetch(api.authurl+'/order_items/'+order_item.id+'/', {
method: 'PUT',
headers: api.httpHeaders,
body: JSON.stringify(order_item)
});
This error shows up
AttributeError: 'AnonymousUser' object has no attribute 'profile'
The print statement identifies these for a GET then a POST request respectively:
[19/Jun/2020 21:03:02] "GET /sellers/3/ HTTP/1.1" 200 196
Current User AD list
[19/Jun/2020 21:03:03] "GET /order_items/ HTTP/1.1" 200 1046
Current User AnonymousUser update
So I have reason to believe that when I make a get request, the authenticated user is detected, but with a PUT it's suddenly Anonymous. I doubt I have to make frontend authentication right? e.g having Authorization with a token in my headers in the request. Since I have that GET request doing fine.
EDIT: adding SellerViewSet:
class SellerViewSet(mixins.RetrieveModelMixin, viewsets.GenericViewSet):
queryset = Seller.objects.all()
serializer_class = SellerSerializer