Django Rest框架序列化程序POST数据未接收数组

Django Rest框架序列化程序POST数据未接收数组,django,django-rest-framework,Django,Django Rest Framework,我正在使用DRF测试api测试我的序列化程序,例如,我将我的数据如下所示: image_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../media/', 'product_images/', 'myimage.png') with open(image_path) as image: encoded_image = base64.b64decode(image.read())

我正在使用DRF测试api测试我的序列化程序,例如,我将我的数据如下所示:

image_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../media/', 'product_images/', 'myimage.png')
    with open(image_path) as image:
        encoded_image = base64.b64decode(image.read())
        data = {
            u'small_name': u'Product Test Case Small Name',
            u'large_name': u'Product Test Case Large Name',
            u'description': u'Product Test Case Description',
            u'images': [
                {u'image': encoded_image}
            ],
            u'variants': [
                {u'value': u'123456789'}
            ]
        }
response = self.client.post(url, data, 'multipart')
但是,当我在相应的序列化程序上接收数据时,
变体
数组和
图像
数组为空

这里可能有什么问题?

您必须将
客户端的
设置为
JSON

response = self.client.post(url, data, format='json')

尝试使用
base64.b64encode()
而不是
base64.b64decode()
方法对图像进行编码

encoded_image = base64.b64encode(image.read())
另外,发送内容类型设置为
application/json
的json编码数据

self.client.post(url, json.dumps(data), content_type='application/json')

您不能同时拥有图像上载和嵌套数据。
或者您想要上传图像并使用多部分编码(HTML表单),这意味着您的数组将无法工作(除非它是一个外键列表或类似列表)。这可能适用于JSon内容类型,但随后您将失去上传图像的能力。您可以选择使用base64编码图像并上传,但您必须在序列化程序上自定义一些内容。

多亏了Linovia和Rahul Gupta,我才能够解决图像和阵列问题。我将序列化程序更改为使用
django-extra-fields
提供的序列化程序接收Base64ImageField。拉胡尔还指出了我代码中的一个愚蠢错误。 我会留下一些代码来帮助有同样问题的人

serializers.py文件

from drf_extra_fields.fields import Base64ImageField


class ProductImageSerializer(serializers.ModelSerializer):
    source = Base64ImageField(source='image', required=False, )
image_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../media/', 'product_images/', 'myimage.png')
with open(image_path) as image:
    encoded_image = base64.b64encode(image.read())
        data = {
                u'small_name': u'Product Test Case Small Name',
                u'large_name': u'Product Test Case Large Name',
                u'description': u'Product Test Case Description',
                u'images': [
                    {u'source': encoded_image}
                ],
                u'variants': [
                    {u'value': u'123456789'}
                ]
            }

response = self.client.post(url, data, format='json')
test.py文件

from drf_extra_fields.fields import Base64ImageField


class ProductImageSerializer(serializers.ModelSerializer):
    source = Base64ImageField(source='image', required=False, )
image_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../media/', 'product_images/', 'myimage.png')
with open(image_path) as image:
    encoded_image = base64.b64encode(image.read())
        data = {
                u'small_name': u'Product Test Case Small Name',
                u'large_name': u'Product Test Case Large Name',
                u'description': u'Product Test Case Description',
                u'images': [
                    {u'source': encoded_image}
                ],
                u'variants': [
                    {u'value': u'123456789'}
                ]
            }

response = self.client.post(url, data, format='json')

请尝试
self.client.post(url,json.dumps(数据),content_type='application/json')
是否包含图像文件或base64编码字符串?(如果是图像文件,上述解决方案将不起作用)。是的,我正在用base64字符串对图像进行编码。然后,它应该与
json
内容类型一起工作。问题可能与编码图像字符串有关。请添加用于编码图像的代码。当然,我将使用图像代码对其进行编辑。我遇到了一个新问题,使用此代码时,我遇到了UnicodeDecor错误,可能是因为我将图像与数据一起发送?@Rodrigaraújovalent尝试使用
base64.b64encode
而不是
base64.b64decode
。抱歉,我忘记了更新以更新状态,我已经解决了这个问题,我现在正在为我的序列化程序使用Base64ImageField,并且我更改了您的建议。谢谢,太好了!很乐意帮忙。