【发布时间】:2020-10-08 19:36:30
【问题描述】:
我想运行一个创建“组”的 POST 调用。假设“人”都将存在。即使不是,错误也不是问题。
class Group(models.Model):
title = models.CharField(max_length=70)
persons = models.ManyToManyField(to=Person, blank=True)
file = models.FileField(upload_to=file_location, null=True, blank=True)
class GroupSerializer(serializers.ModelSerializer):
persons = serializers.PrimaryKeyRelatedField(many=True, queryset=Person.objects.all())
class Meta:
model = Group
fields = '__all__'
如果我发送 JSON 格式
{
"title": "Drama Club",
"persons": [1,2,3]
}
它会起作用的。但由于我无法上传文件,所以我使用 FORM-DATA。
title: Drama Club
persons: [1,2,3]
file: <whatever the format is>
现在问题来了。这是行不通的。它返回此错误
{
"persons": [
"Incorrect type. Expected pk value, received str."
]
}
即使我删除了其他所有内容,并将persons: [1,2,3] 作为表单数据发送,它也会返回相同的错误。
我真的无法理解这种行为。 (我正在使用 POSTMAN 来检查这个)
【问题讨论】:
标签: python django-rest-framework request postman multipartform-data