【发布时间】:2015-12-13 05:44:45
【问题描述】:
输出错误
{
"item_image": [
"The submitted data was not a file. Check the encoding type on the form."
],
"item_thumb": [
"The submitted data was not a file. Check the encoding type on the form."
]
}
我发布的数据是
输入
{
"item_name": "Lural",
"item_image": "/home/prashant/Desktop/suede.png",
"item_thumb": "/home/prashant/Desktop/suede.png",
"item_description": "sd",
"item_mass": 1,
"item_category": "Make Up",
"item_sub_category": "Sub-Feminine",
"item_est_price": "123.12",
"item_wst_price": "120.34"
}
对于媒体类型application/json
views.py
@api_view(['GET', 'POST'])
def product_list(request):
if request.method == 'POST':
serializer = ProductSerializer( data=request.data)
# data.encode("base64")
if serializer.is_valid():
serializer.save()
res_msg = {'Success_Message' : 'Created','Success_Code' : 201}
return Response(res_msg)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
models.py
class Product(models.Model):
item_category_choices = (
('Make Up','Make Up'),
('Skin Care','Skin Care'),
('Fragrance','Fragrance'),
('Personal Care','Personal Care'),
('Hair Care','Hair Care'),
)
item_name = models.CharField(max_length=50)
item_image = models.ImageField()
item_thumb = models.ImageField()
item_description = models.TextField(max_length=200)
item_mass = models.IntegerField()
item_category = models.CharField(max_length=20,choices = item_category_choices)
item_sub_category = models.CharField(max_length=20)
item_est_price = models.DecimalField(max_digits=15,decimal_places=2)
item_wst_price = models.DecimalField(max_digits=15,decimal_places=2)
def __unicode__(self):
return self.item_name or _('Sprint ending %s')% self.item_avg_price
serializers.py
class ProductSerializer(ModelSerializer):
class Meta:
model = Product
fields = ('id','item_name' ,'item_image','item_thumb','item_description','item_mass','item_category',
'item_sub_category','item_est_price','item_wst_price',)
也尝试了许多论坛和第三方软件包,但他们并没有解决这个问题。 GET 也运行良好。
感谢您的宝贵时间
【问题讨论】:
-
嘿,你找到解决这个问题的方法了吗?
-
我也在寻找解决方案。有人找到了吗?
标签: python django django-models django-rest-framework