微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

当我发布文件数据时,它包含空值

如何解决当我发布文件数据时,它包含空值

def image_saving_product(instance,filename):
    return '/'.join( ['product',str(instance.id),filename] )

models.py

class Image(models.Model):
   image_file = models.ImageField(blank=True,upload_to=image_saving_product,null=False)
   product = models.ForeignKey(ProductInfo,on_delete=models.PROTECT,default=None,blank=False)
   def __str__(self):
       return f'image id is {self.id} & this image is for {self.product.id},s product'
   def __unicode__(self):
       return f'image id is {self.id} & this image is for {self.product.id},s product'

serializers.py

class ImageSerializer(serializers.ModelSerializer):

class Meta:
    model = Image
    fields = '__all__'

views.py

class ProductimageViewSet(viewsets.ModelViewSet):
   authentication_classes = [TokenAuthentication]
   permission_classes = [IsAdminUser]
   queryset = Image.objects.all()
   serializer_class = ImageSerializer

enter image description here

这是我的代码,当我想发布图片时我可以发送发布请求,但它的值为空.. 如果您回答我的问题,我将不胜感激。

解决方法

您发送的是 Image NOT image_file,因此在 postman 中更改它并会正常工作。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。