我将带有其他字段的谎言上传到DJANGO testy pie并获得验证错误
如果我发布文件字段和字段字段 - 字段变为来自' test'到[测试]'
如果我也发布了例如57的整数字段,则变为[' 57']
我检查一下 - 没关系
在后面请求.POST已转换数据...模型中的字段描述为小整数字段
class SpkPhotoResource(ModelResource):
img = fields.FileField(attribute="filename", null=True, blank=True)
class Meta:
queryset = SpkPhoto.objects.all()
validation = CleanedDataFormValidation(form_class=forms.SpkPhotoForm)
...
def dehydrate_img(self, bundle):
return repr(bundle.obj.filename.name)
def deserialize(self, request, data, format=None):
if format is None:
format = request.META.get('CONTENT_TYPE','application/json')
if format == 'application/x-www-form-urlencoded':
return request.POST
elif format.startswith('multipart'):
data = request.POST.copy()
data.update(request.FILES)
return data
return super(SpkPhotoResource, self).deserialize(request, data, format)
//模型:
class SpkPhoto(models.Model):
....
filename = models.ImageField(db_column='FILENAME', max_length=256, upload_to=getFilePath, null=False, blank=False)
site_order = models.SmallIntegerField(db_column='SITE_ORDER', blank=True, null=True)
class Meta:
...
def save(self, *args, **kwargs):
p = str('media/OFFICE/' + str(self.spk_propertyid.office.id))
self.filename.storage = FileSystemStorage(location = p )
if not self.spk_photoid:
self.spk_photoid = get12charid_spk_photoid()
super(SpkPhoto, self).save(*args, **kwargs)
def delete(self, *args, **kwargs):
p = str('media/OFFICE/' + str(self.spk_propertyid.office.id))
self.filename.storage = FileSystemStorage(location = p )
self.filename.delete()
super(SpkPhoto, self).delete(*args, **kwargs)
答案 0 :(得分:0)
FormData multipart -Ajax-post-DJANGO-tastie-deserialize querydict中的数据为数组,因此只需从反序列化中重新生成返回数据即可
def deserialize(self, request, data, format=None):
if format is None:
format = request.META.get('CONTENT_TYPE','application/json')
if format == 'application/x-www-form-urlencoded':
return request.POST
elif format.startswith('multipart'):
data = request.POST.copy()
data.update(request.FILES)
data2 = {}
for name, value in data.items():
data2[name] = value
return data2
return super(SpkPhotoResource, self).deserialize(request, data, format)