使用TastyPie尝试发布 * 表单数据时,我收到以下错误消息(见下文):
无法解码JSON对象
我明白我需要在体内传递一个json对象才能使用,但如果我只有表单帖子并且不想使用json(仅在json中输出),那该怎么办呢?
如何使用 FORM POST 制作美味的馅饼?
由于
class SMSResource(ModelResource):
class Meta(CommonMeta):
queryset = Batch.objects.all()
resource_name = 'sms'
list_allowed_methods = ['get', 'post']
detail_allowed_methods = ['get']
答案 0 :(得分:3)
确保您的内容类型为x-www-form-urlencoded以发布帖子并尝试:
class MultipartResource(object):
def deserialize(self, request, data, format=None):
if not format:
format = request.META.get('CONTENT_TYPE', 'application/json')
if format == 'application/x-www-form-urlencoded':
return request.POST
if format.startswith('multipart'):
data = request.POST.copy()
data.update(request.FILES)
return data
return super(MultipartResource, self).deserialize(request, data, format)
def put_detail(self, request, **kwargs):
if request.META.get('CONTENT_TYPE').startswith('multipart') and \
not hasattr(request, '_body'):
request._body = ''
return super(MultipartResource, self).put_detail(request, **kwargs)
然后在您的资源类中:
class SMSResource(MultipartResource, ModelResource):
class Meta(CommonMeta):
queryset = Batch.objects.all()
resource_name = 'sms'
list_allowed_methods = ['get', 'post']
detail_allowed_methods = ['get']