在我的Django站点中,我创建了一个自定义用户模型。我正在使用Windows身份验证,所以到目前为止,用户一直可以直接访问我的站点。我也可以在管理页面中创建用户,并在那里编辑他们的权限。似乎突然我收到了错误Violation of UNIQUE KEY constraint 'UQ__accounts__AB6E616413786680'. Cannot insert duplicate key in object 'dbo.accounts_ouser'. The duplicate key value is ().
,该错误似乎是在数据库端,因为即使使用服务器管理工作室,我也无法插入新的用户。
class OUser(AbstractBaseUser,PermissionsMixin):
email = models.EmailField(verbose_name='email address',max_length=255,unique=True)
username = models.CharField(max_length=150,unique=True)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False) # a superuser
first_name = models.CharField(max_length=100,blank=True,default='')
last_name = models.CharField(max_length=100,default='')
date_joined = models.DateField(auto_now=True)
password = models.CharField(max_length=100)
REQUIRED_FIELDS = [] # Email & Password are required by default.
username_FIELD = 'username'
def __str__(self):
return self.username
def has_perm(self,perm,obj=None):
"Does the user have a specific permission?"
# Simplest possible answer: Yes,always
return True
def has_module_perms(self,app_label):
"Does the user have permissions to view the app `app_label`?"
# Simplest possible answer: Yes,always
return True
另一个奇怪的事情是使用UNIQUE KEY约束,因为列id应该是PRIMARY KEY约束,而ouser模型没有实现任何其他键。 (我不确定这是否很常见,但对我来说似乎很奇怪)
我希望网站恢复到原来的状态,如果用户在Windows上进行了身份验证,则可以在此访问它。
有人知道为什么会这样吗?