【发布时间】:2021-11-19 09:29:01
【问题描述】:
我对我的用户模型进行了一些更改,结果似乎当新用户尝试注册时,他们收到了错误:
IntegrityError at /accounts/signup/
duplicate key value violates unique constraint "users_user_username_key"
我不确定是什么导致了错误
这是forms.py:
from django.contrib.auth import get_user_model, forms
from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _
User = get_user_model()
class UserChangeForm(forms.UserChangeForm):
class Meta(forms.UserChangeForm.Meta):
model = User
class UserCreationForm(forms.UserCreationForm):
error_message = forms.UserCreationForm.error_messages.update(
{
"duplicate_username": _(
"This username has already been taken."
)
}
)
class Meta(forms.UserCreationForm.Meta):
model = User
def clean_username(self):
username = self.cleaned_data["username"]
try:
User.objects.get(username=username)
except User.DoesNotExist:
return username
raise ValidationError(
self.error_messages["duplicate_username"]
)
settings.py
# django-allauth
# ------------------------------------------------------------------------------
ACCOUNT_ALLOW_REGISTRATION = env.bool(
"DJANGO_ACCOUNT_ALLOW_REGISTRATION", True
)
# https://django-allauth.readthedocs.io/en/latest/configuration.html
ACCOUNT_AUTHENTICATION_METHOD = "username"
# https://django-allauth.readthedocs.io/en/latest/configuration.html
ACCOUNT_EMAIL_REQUIRED = True
# https://django-allauth.readthedocs.io/en/latest/configuration.html
ACCOUNT_EMAIL_VERIFICATION = "mandatory"
# https://django-allauth.readthedocs.io/en/latest/configuration.html
ACCOUNT_ADAPTER = "pharmhand.users.adapters.AccountAdapter"
# https://django-allauth.readthedocs.io/en/latest/configuration.html
SOCIALACCOUNT_ADAPTER = (
"pharmhand.users.adapters.SocialAccountAdapter"
)
# Your stuff...
# ------------------------------------------------------------------------------
ACCOUNT_SIGNUP_FORM_CLASS = 'users.forms.UserCreationForm'
还有models.py
from django.contrib.auth.models import AbstractUser
from django.db import models
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
class User(AbstractUser):
# First Name and Last Name Do Not Cover Name Patterns
# Around the Globe.
name = models.CharField(
_("Name of User"), blank=True, max_length=255
)
title = models.CharField(
_("Title"), null=True, max_length=10, default='Dr'
)
first_name = models.CharField(
_("First Name"), null=True, max_length=255, default='test'
)
last_name = models.CharField(
_("Last Name"), null=True, max_length=255, default='Test'
)
job_title = models.CharField(
_("Job Title"), null=True, max_length=255, default='Test'
)
address1 = models.CharField(
_("Address 1"), null=True, max_length=255, default='Test'
)
address2 = models.CharField(
_("Address 2"), null=True, max_length=255, default='Test'
)
address3 = models.CharField(
_("Address 3"), null=True, max_length=255, default='Test'
)
city = models.CharField(
_("City"), null=True, max_length=255, default='Test'
)
post_code = models.CharField(
_("Post Code"), null=True, max_length=255, default='Test'
)
def get_absolute_url(self):
return reverse(
"users:detail", kwargs={"username": self.username}
)
非常感谢任何关于如何避免错误的建议,我不知道混合 contrib.auth allauth 方法是否会使事情变得复杂
【问题讨论】:
-
来自他们的 github 问题页面:如果您收到零星的 IntegrityError,则可能是同一请求被触发了两次,例如由于用户不小心双击了注册按钮。您可能想尝试使用一些 JS 禁用提交按钮...
-
这不是真正的用户——只是我测试,每次我尝试注册时都会发生
-
你为用户模型使用信号吗??
-
@EneP 谢谢,我不这么认为 - 模型代码在问题中
标签: python django django-forms django-allauth