当我 post 文件数据时,它包含空值
when I post file data, it contains null value
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
这是我的代码,当我想要 post 图片时我可以发送 post 请求,但它的值为 null..
如果您回答我的问题,我将不胜感激。
您发送的是 Image
而不是 image_file
,因此请在 postman 中进行更改,这样就可以正常工作了。
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
这是我的代码,当我想要 post 图片时我可以发送 post 请求,但它的值为 null.. 如果您回答我的问题,我将不胜感激。
您发送的是 Image
而不是 image_file
,因此请在 postman 中进行更改,这样就可以正常工作了。