There are "Consultant" and "Price" tables in DB and "Price" has a foreign key to "Consultant". I want to get all price records that are related to specific consultant. But I get an error when I use APITestCase to send GET request.
views.py:
class PriceAPI(APIView):
serializer_class = PriceSerializer
def get(self, request):
consultant_type = request.data.get('type', None)
try:
consultant = Consultant.objects.get(user=request.user, type=consultant_type)
except Consultant.DoesNotExist:
return Response(status=status.HTTP_406_NOT_ACCEPTABLE)
try:
serializer = self.serializer_class(consultant.prices, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
except:
return Response(status=status.HTTP_400_BAD_REQUEST)
test.py:
class PriceTest(APITestCase):
def setUp(self):
###
def test_get_delete_price(self):
response = self.client.get(
reverse('price'),
data=json.dumps(
{'type': 'sports'}),
content_type='application/json'
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
I get this error:
Error
Traceback (most recent call last):
File "D:\programming\Pycharm\Projects\Django Projects\CAPP\capp_api\tests.py", line 394, in test_get_delete_price
content_type='application/json'
File "D:\programming\Pycharm\Projects\Django Projects\CAPP\venv\lib\site-packages\rest_framework\test.py", line 286, in get
response = super().get(path, data=data, **extra)
File "D:\programming\Pycharm\Projects\Django Projects\CAPP\venv\lib\site-packages\rest_framework\test.py", line 194, in get
'QUERY_STRING': urlencode(data or {}, doseq=True),
File "D:\programming\Pycharm\Projects\Django Projects\CAPP\venv\lib\site-packages\django\utils\http.py", line 113, in urlencode
for key, value in query:
ValueError: not enough values to unpack (expected 2, got 1)
This error is about data that is sent in request. How can I do that in GET request?