这是我的UserLoginResource:
class UserLoginResource(ModelResource):
class Meta:
object_class = User
queryset = User.objects.all()
allowed_methods = ['post','get']
include_resource_uri = False
resource_name = 'login'
excludes = ['is_active','is_staff','is_superuser']
authentication = SillyAuthentication()
authorization = SillyAuthorization()
def obj_create(self, bundle, request=None, **kwargs):
try:
bundle = super(UserLoginResource, self).obj_create(bundle,request,**kwargs)
bundle.obj.set_password(bundle.data.get('password'))
bundle.obj.set_username(bundle.data.get('username'))
bundle.obj.save()
return bundle
except IntegrityError:
raise BadRequest('The username exists')
def dehydrate(self,bundle):
bundle.data['custom_field'] = "Whatever you want"
return bundle
如果我使用已存在的用户名和密码发布,则会出现500错误,指出完整性错误,并且用户名已存在。
答案 0 :(得分:0)
使用此代码创建用户对象而不是登录
请参阅tastypie.validation