我正在使用一个用于创建新用户的资源
class CreateUserResource(ModelResource):
class Meta:
allowed_methods = ['post']
object_class = User
authentication = Authentication()
authorization = Authorization()
include_resource_uri = False
fields = ['username']
resource_name = 'createuser'
def obj_create(self, bundle, request=None, **kwargs):
try:
bundle = super(CreateUserResource, self).obj_create(bundle, request, **kwargs)
bundle.obj.set_password(bundle.data.get('password'))
bundle.obj.save()
except IntegrityError:
raise BadRequest('That username already exists')
return bundle
当我使用它使用jquery它工作正常并添加新用户,但我试图在views.py中使用它。并找不到如何将请求或捆绑值传递给此资源的帮助。
我的观点看起来像这样
newuser = CreateUserResource()
ur_bundle = newuser.build_bundle(data = '{username:"aaa"}', request=request)
newuser.obj_create(ur_bundle)
但是提供了包错误