0

这是我的用户登录资源:

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 错误,提示完整性错误,并且用户名已经存在。

  1. 如何登录?
  2. 即使有错误(假设用户名不存在,我如何美化错误响应?
4

1 回答 1

0
  1. 使用此代码,您正在创建用户对象而不是登录

  2. 见 sweetpie.validation

于 2012-10-04T16:11:58.457 回答