Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
IntegrityError:使用django tasty pie登录用户时,重复的键值违反了唯一约束_Django_Tastypie - Fatal编程技术网

IntegrityError:使用django tasty pie登录用户时,重复的键值违反了唯一约束

IntegrityError:使用django tasty pie登录用户时,重复的键值违反了唯一约束,django,tastypie,Django,Tastypie,这是我的UserLoginResource: class UserLoginResource(ModelResource): class Meta: object_class = User queryset = User.objects.all() allowed_methods = ['post','get'] include_resource_uri = False resource_name = 'l

这是我的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错误,表示完整性错误,并且用户名已经存在

  • 如何登录
  • 即使有错误(假设用户名不存在,如何美化错误响应
  • 使用此代码,您正在创建用户对象,而不是登录

  • 请参阅tastype.validation