【问题标题】:when I post file data, it contains null value当我发布文件数据时,它包含空值
【发布时间】:2021-10-02 02:06:43
【问题描述】:
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'

序列化器.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

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

【问题讨论】:

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


    【解决方案1】:

    您发送 Image 而不是 image_file 所以在 postman 中更改它并且可以正常工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-30
      • 1970-01-01
      • 2012-11-09
      • 2018-04-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多