gpt4 book ai didi

python - django tastypie 和 xml 错误消息 : Please check your `` formats `` and ` `content_types`` on your Serializer

转载 作者:行者123 更新时间:2023-12-01 04:06:49 25 4
gpt4 key购买 nike

我正在使用 tastypie,但遇到了这个错误:

The format indicated 'application/x-www-form-urlencoded' had no available deserialization method. Please check your formats and content_types on your Serializer.

我不知道这意味着什么。

这是什么意思以及如何解决这个问题?

在 api.py 中

class UserResource(ModelResource):
class Meta:
queryset = User.objects.all()
fields = ['username', 'id']
include_resource_uri = False
allowed_methods = ['get']


class PostResource(ModelResource):
class Meta:
queryset = Post.objects.all()
include_resource_uri = False
allowed_methods = ['get']
filtering = {
"id": ALL,
}


class CommentResource(ModelResource):
post = fields.ForeignKey(PostResource, 'post')
writer = fields.ForeignKey(UserResource, 'writer', full=True, readonly=True)
parent_comment = fields.ForeignKey('main.api.CommentResource', 'parent_comment', null=True)

class Meta:
queryset = Comment.objects.all()
authorization = Authorization()
include_resource_uri = False
ordering = ['-pub_date']
filtering = {
'post': ALL_WITH_RELATIONS,
'comment': ALL_WITH_RELATIONS,
'parent_comment': ALL_WITH_RELATIONS,
}

在 urls.py 中

from tastypie.api import Api
from main.api import CommentResource, UserResource, PostResource


v1_api = Api(api_name='v1')
v1_api.register(CommentResource())
v1_api.register(UserResource())
v1_api.register(PostResource())

这是回溯...不确定是否有帮助

Traceback (most recent call last): 
File "env/local/lib/python2.7/site-packages/tastypie/resources.py", line 211, in wrapper response = callback(request, *args, **kwargs)
File "env/local/lib/python2.7/site-packages/tastypie/resources.py", line 438, in dispatch_list return self.dispatch('list', request, **kwargs)
File "env/local/lib/python2.7/site-packages/tastypie/resources.py", line 470, in dispatch response = method(request, **kwargs)
File "env/local/lib/python2.7/site-packages/tastypie/resources.py", line 1362, in post_list deserialized = self.deserialize(request, request.body, format=request.META.get('CONTENT_TYPE', 'application/json'))
File "env/local/lib/python2.7/site-packages/tastypie/resources.py", line 387, in deserialize deserialized = self._meta.serializer.deserialize(data, format=request.META.get('CONTENT_TYPE', format))
File "env/local/lib/python2.7/site-packages/tastypie/serializers.py", line 267, in deserialize raise UnsupportedFormat("The format indicated '%s' had no available deserialization method.
Please check your ``formats`` and ``content_types`` on your Serializer." % format)
UnsupportedFormat: The format indicated 'application/x-www-form-urlencoded' had no available deserialization method.

Please check your ``formats`` and ``content_types`` on your Serializer.

最佳答案

看起来您正在尝试将 HTML 表单数据提交到 tastypie。 application/x-www-form-urlencoded 是 HTML 表单数据类型的 mimetype/contenttype,另一个是 multipart/form-data。您可能应该将输入格式设置为 JSON 或 XML。

如果您需要接受来自 HTML 表单的数据,tastypie 可能不是满足您需求的最佳选择。

以下是一些选项:

  1. 使用常规 Django 表单和 View ,不使用 tastypie。
  2. 在 Django View 中使用 tastypie 资源: http://django-tastypie.readthedocs.org/en/v0.13.3/cookbook.html#using-your-resource-in-regular-views
  3. 编写表单数据序列化器:https://stackoverflow.com/a/14075440/241955

关于python - django tastypie 和 xml 错误消息 : Please check your `` formats `` and ` `content_types`` on your Serializer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35451086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com