2012-05-21 42 views
0

我正在使用Django和TastyPie創建一個API。我正嘗試通過資源註冊用戶。我把我的大部分代碼這個問題,有一個類似的目標:Django Tastypie用戶註冊

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

我的問題是,註冊用戶時,我得到的一個問題。

的代碼是:

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(我這樣做,編程)與兩個用戶名和密碼參數,不過,我得到以下錯誤回:

{"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. 
"} 

我可以推斷出序列化程序存在一些問題,但是哪些以及如何解決?

謝謝

+0

我試過了,它不會失敗,再次提交相同的用戶名。實際上它使用相同的用戶名再次創建用戶。想知道爲什麼.. – pyeleven

+0

我明白了。你能告訴我你是如何執行POST請求的? –

+0

發佈一個基於http://django-tastypie.readthedocs.org/en/latest/interacting.html#creating-a-new-resource-post的用戶名和密碼的json。此外,我正在使用django-nonrel和appengine,所以這也可能是問題所在。 WIll將在下週進行調查.. – pyeleven

回答

2

我猜你要使用django.test.client.post與Tastypie。如果是這樣,你需要傳入一個額外的參數 - content_type。這裏是你的電話應該看起來如何:

client.post('/resource/to/create/', 'json_string_here', content_type='application/json') 
+0

謝謝你的回覆。我會盡快嘗試並回復。 –

1

有同樣的問題。在頭文件中傳遞「Content-Type:application/json」爲我解決了它。