Python ErrorDetail(字符串=';应为项目列表,但得到类型“ReturnDict”';,代码=';非#列表&#

Python ErrorDetail(字符串=';应为项目列表,但得到类型“ReturnDict”';,代码=';非#列表&#,python,django,unit-testing,django-rest-framework,factory,Python,Django,Unit Testing,Django Rest Framework,Factory,当我测试ThankYouMessage时,我得到错误{'images':{'non_field_errors':[ErrorDetail(string='应为项目列表,但得到类型为“ReturnDict.”,code='not_a_list')]} 测验 模型 工厂 串行器 如果我写image\u data=ThankYouMessageImageSerializer(self.Thankyou\u image,many=True)。data我得到TypeError:“ThankyImage”对

当我测试
ThankYouMessage
时,我得到错误
{'images':{'non_field_errors':[ErrorDetail(string='应为项目列表,但得到类型为“ReturnDict.”,code='not_a_list')]}

测验 模型 工厂 串行器
如果我写
image\u data=ThankYouMessageImageSerializer(self.Thankyou\u image,many=True)。data
我得到
TypeError:“ThankyImage”对象不可编辑
你的
ThankYouMessage
将有一个相关字段
图像
作为
ThankYouImage
的集合,所以你需要你的
谢谢你的图片作为列表。您可以使用
.build\u batch()
工厂方法构建它,并使用
many=True
参数序列化它

类ThankYouMessagesSerializerTestCase(中间测试用例,测试用例):
def设置(自身)->无:
...
self.Thankyou\u images=ThankYouImageFactory.build\u批处理(3)
def测试\u谢谢\u消息\u反序列化(自我)->无:
image\u data=ThankYouMessageImageSerializer(self.Thankyou\u images,many=True)。数据
...
当我测试它时,您的
image\u数据仍然无法序列化并显示此错误:

提交的数据不是文件。检查表单上的编码类型

但我认为这是另一个话题


顺便说一句,为什么不直接序列化生成的
self.thank\u message
而不是
thank\u data
?你想在这里测试什么

class ThankYouMessagesSerializerTestCase(MediaTestCase, TestCase):
    def setUp(self) -> None:
        self.thank_you_message = ThankYouMessageFactory()
        self.thank_you_image = ThankYouImageFactory()

    def test_thank_you_message_deserialize(self) -> None:
        image_data = ThankYouMessageImageSerializer(self.thank_you_image).data
        thank_you_data = ({'text': 'Some text', 'images': image_data})
        serializer = ThankYouMessagesSerializer(data=thank_you_data)
        serializer.is_valid()
        print(serializer.errors)
        # {'images': {'non_field_errors': [ErrorDetail(string='Expected a list of items but got type "ReturnDict".', code='not_a_list')]}}
        self.assertTrue(serializer.is_valid())
class ThankYouMessage(models.Model):
    donation = models.ForeignKey("donation.Donation", on_delete=models.CASCADE, related_name='thank_message', unique=True)
    text = models.TextField()


class ThankImage(models.Model):
    message = models.ForeignKey("donation.ThankYouMessage", on_delete=models.CASCADE, related_name='images')
    image = models.ImageField(upload_to="thankmessageimages/")
class ThankYouMessageFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = ThankYouMessage

    donation = factory.SubFactory(DonationFactory)
    text = factory.Sequence(lambda n: f"Thank you {n}")
When I test При тестировании сериализатора получаю ошибку

class ThankYouImageFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = ThankImage

    image = factory.django.ImageField(name=f"testimage.jpeg", color="blue")
    message = factory.SubFactory(ThankYouMessageFactory)
class ThankYouMessageImageSerializer(ModelSerializer):
    class Meta:
        model = ThankImage
        fields = '__all__'
        read_only_fields = ("message", "id")


class ThankYouMessagesSerializer(ModelSerializer):
    images = ThankYouMessageImageSerializer(many=True)
    donation = serializers.CharField(source='donation.id', read_only=True)
    donor_id = serializers.CharField(source='donation.donor.id', read_only=True)

    class Meta:
        model = ThankYouMessage
        fields = 'text', 'donation', 'donor_id', 'images'