2012-10-04 94 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

相關問題