django tastypie 和 xml 错误消息:请检查序列化程序上的“格式”和“content_types”

django tastypie and xml error message: Please check your ``formats`` and ``content_types`` on your Serializer

我正在使用 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.

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

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

这里有一些选项:

  1. 使用常规的 Django 表单和视图,不使用 tastypie。
  2. 在 Django 视图中使用 tastypie 资源: http://django-tastypie.readthedocs.org/en/v0.13.3/cookbook.html#using-your-resource-in-regular-views
  3. 编写表单数据序列化程序: