首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Django Tastypie用户注册

Django Tastypie用户注册
EN

Stack Overflow用户
提问于 2012-05-22 04:34:46
回答 2查看 3.3K关注 0票数 0

我正在使用Django和TastyPie创建一个API。我正在尝试通过资源注册用户。我从这个有类似目标的问题中提取了大部分代码:

How to create or register User using django-tastypie API programmatically?

我的问题是,我在注册用户时遇到了一个问题。

代码是:

代码语言:javascript
复制
class RegisterUserResource(ModelResource):
    class Meta:
        allowed_methods = ['post']
        object_class = VouchersUser

        authentication = Authentication()
        authorization = Authorization()

        include_resource_uri = False
        fields = ['username']

        resource_name = 'register'

    def obj_create(self, bundle, request=None, **kwargs):
        try:
            bundle = super(RegisterUserResource).obj_create(bundle, request, **kwargs)
            bundle.obj.set_password(bundle.data.get('password'))
            bundle.obj.save()
        except IntegrityError:
            raise BadRequest('User with this username already exists')
        return bundle

但是,当我发送一个带有用户名和密码参数的POST时(我是通过编程实现的),我得到了以下错误:

代码语言:javascript
复制
{"error_message": "The format indicated 'multipart/form-data' had no available deserialization method. Please check your formats and content_types on your Serializer.", "traceback": "Traceback (most recent call last):

 File "/Library/Python/2.7/site-packages/django_tastypie-0.9.11-py2.7.egg/tastypie/resources.py", line 195, in wrapper
 response = callback(request, *args, **kwargs)

 File "/Library/Python/2.7/site-packages/django_tastypie-0.9.11-py2.7.egg/tastypie/resources.py", line 402, in dispatch_list
 return self.dispatch('list', request, **kwargs)

 File "/Library/Python/2.7/site-packages/django_tastypie-0.9.11-py2.7.egg/tastypie/resources.py", line 431, in dispatch
 response = method(request, **kwargs)

 File "/Library/Python/2.7/site-packages/django_tastypie-0.9.11-py2.7.egg/tastypie/resources.py", line 1176, in post_list
 deserialized = self.deserialize(request, request.raw_post_data, format=request.META.get('CONTENT_TYPE', 'application/json'))

 File "/Library/Python/2.7/site-packages/django_tastypie-0.9.11-py2.7.egg/tastypie/resources.py", line 351, in deserialize
 deserialized = self._meta.serializer.deserialize(data, format=request.META.get('CONTENT_TYPE', 'application/json'))

 File "/Library/Python/2.7/site-packages/django_tastypie-0.9.11-py2.7.egg/tastypie/serializers.py", line 192, 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 'multipart/form-data' had no available deserialization method. Please check your formats and content_types on your Serializer.
"}

我可以推断出序列化程序有一些问题,但是我该如何解决它呢?

谢谢

EN

回答 2

Stack Overflow用户

发布于 2012-05-24 23:32:59

我猜您正在尝试将django.test.client.post与Tastypie一起使用。如果是这样的话,您需要传入一个额外的参数- content_type。下面是您的调用应该是什么样子的:

代码语言:javascript
复制
client.post('/resource/to/create/', 'json_string_here', content_type='application/json')
票数 2
EN

Stack Overflow用户

发布于 2014-06-26 03:14:35

也有同样的问题。在header中传递"Content-Type: application/json“为我解决了这个问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10692235

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档