'TypeError: Field 'id' expected a number but got <django.contrib.auth.models.AnonymousUser object at 0x7f5e95756920>

first time I ask on the website.

I was testing a Django Restframework app. The is to check an unauthenticated user that wants to create a review. I got this error: TypeError: Field 'id' expected a number but got <django.contrib.auth.models.AnonymousUser object at 0x7f5e95756920>.

this is the class test: class ReviewTestCase(APITestCase):

def setUp(self):
    self.user = User.objects.create_user(
        username="example", password="Password@123")
    self.token = Token.objects.get(user__username=self.user)
    self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token.key)

    self.stream = models.StreamPlatform.objects.create(
        name="Netflix",
        about="#1 Platform",
        website="https://www.netflix.com"
    )
    self.watchlist = models.WatchList.objects.create(
        platform=self.stream, title="Example Movie",
        storyline="Example Movie",
        active=True
    )
    self.watchlist2 = models.WatchList.objects.create(platform=self.stream, title="Example Movie",
                                                      storyline="Example Movie", active=True)
    self.review = models.Review.objects.create(review_user=self.user, rating=5, description="Great Movie",
                                               watchlist=self.watchlist2, active=True)

def test_review_create(self):
    data = {
        "review_user": self.user,
        "rating": 5,
        "description": "Great Movie!",
        "watchlist": self.watchlist,
        "active": True
    }

    response = self.client.post(
        reverse('reviewcreate', args=(self.watchlist.id,)), data)
    self.assertEqual(response.status_code, status.HTTP_201_CREATED)
    self.assertEqual(models.Review.objects.count(), 2)

    response = self.client.post(
        reverse('reviewcreate', args=(self.watchlist.id,)), data)
    self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
**#The test who cause the error**
def test_review_create_unauth(self):
    data = {
        "review_user": self.user,
        "rating": 5,
        "description": "Great Movie!",
        "watchlist": self.watchlist,
        "active": True
    }

    self.client.force_authenticate(user=None, token=None)
    response = self.client.post(
        reverse('reviewcreate', args=(self.watchlist.id,)), data)
    self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)

def test_review_update(self):
    data = {
        "review_user": self.user,
        "rating": 4,
        "description": "Great Movie! - Updated",
        "watchlist": self.watchlist,
        "active": False
    }
    response = self.client.put(
        reverse('review-detail', args=(self.review.id,)), data)
    self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_review_list(self):
    response = self.client.get(
        reverse('movie-review', args=(self.watchlist.id,)))
    self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_review_ind(self):
    response = self.client.get(
        reverse('review-detail', args=(self.review.id,)))
    self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_review_ind_delete(self):
    response = self.client.delete(
        reverse('review-detail', args=(self.review.id,)))
    self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)

def test_review_user(self):
    response = self.client.get(
        '/watch/review/?username' + self.user.username)
    self.assertEqual(response.status_code, status.HTTP_200_OK)


Solution 1:[1]

In data items, you are passing an object, and you have to pasa an id. Change this:

review_user: self.user.id

Solution 2:[2]

Thanks to everyone, I found the mistake. In fact the problem was not in the func in the tests.py but in the view, where I misspelled: permission_classes = [IsAuthenticated] with: permission_clasees = [IsAuthenticated] So everything is fine now. Thanks again to all of you

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 LaCharcaSoftware
Solution 2 Cosmo Laurelli