2012-10-04 57 views
0

这是我UserLoginResource在用户登录:IntegrityError:重复键值违反唯一约束,同时使用Django美味馅饼

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. 即使有错误(比方说用户名不存在,我怎么美化错误响应?

回答

0
  1. 与此代码,创建用户对象,而不是登录

  2. 参见tastypie.validation

相关问题