Django Unitest, can't recognize the error

I am wondering if could anyone help out here, I am trying to send a post request to the URL, but I am not sure about the best way of doing it?

The idea here is to test the creation of the card object, but I am getting an error which I am not managing to figure out, if anyone could help me out here it would be fantastic.

here the code and the error below it

  def test_create_card(self):
 
        payload = {
            'title': "Sample card title",
            'description': 'Get a free coffee for every 10 coffee you buy',
            'points_needed': 10,
        }
        res = self.client.post(CARDS_URL, self.company, **payload)

        print(res)

        self.assertEqual(res.status_code, status.HTTP_201_CREATED)
        card = Card.objects.get(id=res.data['id'])
        for k, v in payload.items():
            self.assertEqual(getattr(card, k), v)
        self.assertEqual(card.company, self.company)

ERROR

ERROR: test_create_card (card.tests.test_card_api.PrivateCardAPITests)
Test creating a card

Traceback (most recent call last):
  File "/app/card/tests/test_card_api.py", line 141, in test_create_card
    res = self.client.post(CARDS_URL, self.company, **payload)
  File "/py/lib/python3.9/site-packages/rest_framework/test.py", line 295, in post
    response = super().post(
  File "/py/lib/python3.9/site-packages/rest_framework/test.py", line 208, in post
    data, content_type = self._encode_data(data, format, content_type)
  File "/py/lib/python3.9/site-packages/rest_framework/test.py", line 179, in _encode_data
    ret = renderer.render(data)
  File "/py/lib/python3.9/site-packages/rest_framework/renderers.py", line 914, in render
    return encode_multipart(self.BOUNDARY, data)
  File "/py/lib/python3.9/site-packages/django/test/client.py", line 245, in encode_multipart
    for (key, value) in data.items():
AttributeError: 'Company' object has no attribute 'items

'

and here is the model:

' class Card(models.Model):

company = models.ForeignKey(Company, on_delete=models.CASCADE)
title = models.CharField(max_length=200)
slug = models.SlugField(max_length=200, blank=True)
description = models.TextField(blank=True)
points_needed = models.IntegerField()
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True, blank=True)
active = models.BooleanField(default=True)

def __str__(self):
    return self.title

'

Back to Top