[英]Django Tastypie User Registration
我正在使用Django和DeliciousPie創建API。 我正在嘗試通過資源注冊用戶。 我從這個具有相似目標的問題中提取了大部分代碼:
如何以編程方式使用django-tastypie API創建或注冊用戶?
我的問題是,注冊用戶時遇到問題。
代碼是:
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.
"}
我可以推斷出序列化器存在一些問題,但是該如何解決?
謝謝
我猜您正在嘗試將django.test.client.post
與Deliciouspie一起使用。 如果是這樣,則需要傳入一個額外的參數-content_type。 這是您的通話外觀:
client.post('/resource/to/create/', 'json_string_here', content_type='application/json')
有同樣的問題。 在標頭中傳遞“ Content-Type:application / json”可為我解決。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.