我有
test.py
from rest_framework.test import APITestCase
from django.core.files.uploadedfile import SimpleUploadedFile
from django.urls import reverse
class PostImageTest(APITestCase):
def setUp(self) -> None:
self.product = Product.objects.create(...)
self.sampleImage = SimpleUploadedFile("test_image.jpg", b"binary data", content_type="image/jpeg")
def test_with_valid_data(self) -> None:
data = { "image":self.sampleImage, "is_featured":true }
response = client.post(reverse('images'), data, format='multipart')
我想将像
images?product=id
这样的查询参数传递给client.post()
方法。我也不知道如何编码图像和发送 POST 请求。
当我这样尝试时
response = client.post(reverse('images'), {'product' : self.product.id}, data=data, format='multipart')
它给了我这个错误
TypeError: Client.post() got multiple values for argument 'data'