DRF尝试保存图像会出现错误"提交的数据不是文件"

时间:2018-03-08 10:20:35

标签: python django django-rest-framework

这是Django REST Framework项目。我正在尝试创建包含图片的新产品。我在模型中使用FileField作为图像。我通过POST方法将数据发送到服务器。您可以在下面看到请求有效负载。你可以看到发送的图像是一个文件,但为什么它不能保存并给我错误400:{image: ["The submitted data was not a file. Check the encoding type on the form."]}?我也试过enctype="multipart/form-data",但没有区别。

{
    full_description:"<p>asdasdasd</p>"
    image:File(2835) {
        name: "php.png", lastModified: 1520497841095, 
        lastModifiedDate: Thu Mar 08 2018 12:00:41 GMT+0330 (+0330), 
        webkitRelativePath: "", size: 2835, …}
    mini_description:"dasdasdasd"
    price:"222"
    title:"asdsadasds"
    video_length:"233"
    video_level:"pro"
    you_learn:"asdasdasd"
    you_need:"asdasdasd"
}

,服务器说:

{image: ["The submitted data was not a file. Check the encoding type on the form."]}
image
:
["The submitted data was not a file. Check the encoding type on the form."]

我的观点:

class StoreApiView(mixins.CreateModelMixin, mixins.UpdateModelMixin, generics.ListAPIView):
    lookup_field = 'pk'
    serializer_class = ProductSerializer
    permission_classes = [IsAuthenticatedOrReadOnly]

    def get_queryset(self):
        qs = Product.objects.all()
        query = self.request.GET.get('q')
        if query is not None:
            qs = qs.filter(
                Q(title__icontains=query) |
                Q(mini_description__icontains=query) |
                Q(full_description__icontains=query)
            ).distinct()
        return qs

    def perform_create(self, serializer):
        serializer.save(author=self.request.user)

    def post(self, request, *args, **kwargs):
        if request.method == 'POST':
            file_serial = ProductSerializer(data=request.data)
            if file_serial.is_valid():
                file_serial.save(author_id=request.user.id)
                return Response(file_serial.data, status=status.HTTP_201_CREATED)
            else:
                return Response(file_serial.errors, status=status.HTTP_400_BAD_REQUEST)

相关型号serilizer:

# product
class ProductSerializer(ModelSerializer):
    product_ratings = ProductRatingsSerializer(many=True, read_only=True)
    product_video = ProductVideoSerializer(many=True, read_only=True)
    author = serializers.SerializerMethodField()

    def get_author(self, obj):
        return obj.author.first_name + ' ' + obj.author.last_name

    def get_category(self, obj):
        return obj.category.title

    class Meta:
        model = Product
        fields = [
            'product_id',
            'author',
            'title',
            'mini_description',
            'you_learn',
            'you_need',
            'full_description',
            'price',
            'discount',
            'discount_code',
            'discount_code_precent',
            'video_level',
            'video_length',
            'created_date',
            'updated_date',
            'product_ratings',
            'product_video',
            'image'
        ]
        read_only_fields = ['product_id', 'created_date', 'updated_date', 'author',
                            'product_ratings']

    def get_image(self, obj):
        request = self.context.get('request')
        return request.build_absolute_uri(obj.image.url)

0 个答案:

没有答案