Search code examples
pythonxmldjangotastypie

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


I'm using tastypie and I have met this error:

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

I have no idea what it means.

What does this mean and how do I fix this?

in 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,
        }

in 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())

This is the traceback...not sure if it helps

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.

Solution

  • Looks like you're trying to submit HTML form data to tastypie. application/x-www-form-urlencoded is a mimetype/contenttype for a type of HTML form data, the other being multipart/form-data. You should probably format your input as JSON or XML.

    If you need to accept data from an HTML form, tastypie might not be the best choice for your needs.

    Here are some options:

    1. Use regular Django forms & views, no tastypie.
    2. Use a tastypie resource inside a Django view: http://django-tastypie.readthedocs.org/en/v0.13.3/cookbook.html#using-your-resource-in-regular-views
    3. Write a form data serializer: https://stackoverflow.com/a/14075440/241955