【问题标题】:Django DRF when file is empty while updating it gives error - when file is selected it worksDjango DRF当文件为空时更新它会给出错误 - 选择文件时它可以工作
【发布时间】:2020-11-30 22:06:20
【问题描述】:

我在 DRF 中有一个视图 - 当我选择图像时它可以正常工作,但是当没有选择任何文件时它会引发错误:

The submitted data was not a file. Check the encoding type on the form.code invalid

我也尝试有条件地将图像字段,即(mainimage 和 image_with_self)放在dat1 {} 中,但这也不起作用。它不包括 dat1={} 中的键值对,而是将其放在字典对象之外作为 old[Inmemoryuploaded:filename]
下面是我的代码

class ArtworkViewSet(viewsets.ModelViewSet):
    
    queryset = Artwork.objects.all()
    # import pdb; pdb.set_trace();
    serializer_class = ArtworkSerializer
    permission_classes = [AllowAny]
    
    def partial_update(self, request, *args, **kwargs):
        # import pdb; pdb.set_trace()
        # kwargs['partial'] = True
        partial = kwargs.pop('partial', True)
        instance = self.get_object()
        dat1={}
        if request.data.get('own_work') == "false":
            name_of_creator=request.data.get('name_of_creator')
            name_of_owner=request.data.get('name_of_owner')
            age_of_work=request.data.get('age_of_work')
            house_no=request.data.get('house_no')
            building_name=request.data.get('building_name')
            
        else:

            user_id=request.user.id
            userprofile = UserProfile.objects.get(user=user_id)
            name_of_creator=userprofile.name
            name_of_owner=userprofile.name
            house_no=userprofile.house_no
            building_name=userprofile.building_name
            
      
        # if(request.data['image_with_self']!= ""):
       
        #     dat1['image_with_self']=request.data['image_with_self']

        # if(request.data['mainimage'] != ""):
        
        #     dat1['mainimage']=request.data['mainimage']
       
        

        


        dat1={  

            'title': request.data.get("title"),
           
           'description': request.data.get("description"),
           'artcontest': request.data.get("artcontest"),


            'mainimage':request.data['mainimage'],
            'image_with_self':request.data['image_with_self'],


           'category_id': request.data.get("category"),
           'size': request.data.get("size"),
           'weight': request.data.get("weight"),
           
           'material': request.data.get("material"),
           'sale_lend': request.data.get("sale_lend"),
           'own_work': request.data.get("own_work"),
           'name_of_creator': name_of_creator,
           'name_of_owner': name_of_owner,
           'house_no': house_no,
           'building_name': building_name,
             
            }
        # instance.save()

        import pdb; pdb.set_trace()
         
        serializer= self.get_serializer(instance, dat1, partial=partial)
       
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data)
   

【问题讨论】:

    标签: django django-rest-framework django-file-upload


    【解决方案1】:

    您是否在序列化类中将图像字段设为可选。如果不是,你应该这样做。

    image = serializers.ImageField(required=False, max_length=None, allow_empty_file=True, use_url=True)
    

    几天前我遇到了类似的错误。可能是it可以帮助你。

    【讨论】:

    • 我正在使用ModelSerializer,并且在模型中它已经是blank=True,null=True。此外,在没有上传任何内容时更新图像字段时,它不应替换数据库中的旧文件值。如果我可以在序列化程序中可选地定义/覆盖 required=False,您知道吗?
    • 对于文件字段,将 null 和空白设置为 true 是不够的。您必须使 allow_empty_fill=True。 .... 如果没有传递任何内容,为了保留以前的图像,您需要从消毒器的验证器函数中检查图像字段是否为空。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-14
    • 2019-08-15
    相关资源
    最近更新 更多