每当我启动服务器并尝试访问管理页面时,我经常会收到ValidationError:['test@user.com'值必须为整数]。
我已经为项目创建了自定义用户,并按照文档中的说明进行了必要的设置(admin.py,model.py,forms.py)。我还尝试在settings.py中编写身份验证后端,但导致另一个错误。 但无法弄清楚为什么pk使用USERNAME_FIELD作为pk。
class User(AbstractBaseUser):
user_id = models.AutoField(primary_key=True,unique=True)
username = models.CharField(max_length=50,unique=True)
first_name = models.CharField(max_length=50,blank=True,null=True)
last_name = models.CharField(max_length=50,blank=True,null=True)
email = models.EmailField(
max_length=200,
unique=True,
)
date_joined = models.DateTimeField(default=timezone.now)
# confirm = models.BooleanField(default=False)
# confirm_date = models.DateTimeField(default=False)
# phone_no = models.IntegerField(max_length=40)
state = models.CharField(max_length=50,blank=True,null=True)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
is_admin = models.BooleanField(default=False)
objects = UserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username']
Traceback:
File "C:\Users\Ayo Paul\.virtualenvs\python_and_Django_web_development-NQaQovYW\lib\site-packages\django\db\models\fields\__init__.py" in to_python
940. return int(value)
During handling of the above exception (invalid literal for int() with base 10: 'test@user.com'), another exception occurred:
File "C:\Users\Ayo Paul\.virtualenvs\python_and_Django_web_development-NQaQovYW\lib\site-packages\django\core\handlers\exception.py" in inner
34. response = get_response(request)
File "C:\Users\Ayo Paul\.virtualenvs\python_and_Django_web_development-NQaQovYW\lib\site-packages\django\core\handlers\base.py" in _get_response
126. response = self.process_exception_by_middleware(e, request)
File "C:\Users\Ayo Paul\.virtualenvs\python_and_Django_web_development-NQaQovYW\lib\site-packages\django\core\handlers\base.py" in _get_response
124. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Ayo Paul\.virtualenvs\python_and_Django_web_development-NQaQovYW\lib\site-packages\django\contrib\admin\sites.py" in wrapper
241. return self.admin_view(view, cacheable)(*args, **kwargs)
File "C:\Users\Ayo Paul\.virtualenvs\python_and_Django_web_development-NQaQovYW\lib\site-packages\django\utils\decorators.py" in _wrapped_view
142. response = view_func(request, *args, **kwargs)
File "C:\Users\Ayo Paul\.virtualenvs\python_and_Django_web_development-NQaQovYW\lib\site-packages\django\views\decorators\cache.py" in _wrapped_view_func
44. response = view_func(request, *args, **kwargs)
File "C:\Users\Ayo Paul\.virtualenvs\python_and_Django_web_development-NQaQovYW\lib\site-packages\django\contrib\admin\sites.py" in inner
212. if not self.has_permission(request):
File "C:\Users\Ayo Paul\.virtualenvs\python_and_Django_web_development-NQaQovYW\lib\site-packages\django\contrib\admin\sites.py" in has_permission
186. return request.user.is_active and request.user.is_staff
File "C:\Users\Ayo Paul\.virtualenvs\python_and_Django_web_development-NQaQovYW\lib\site-packages\django\utils\functional.py" in inner
213. self._setup()
File "C:\Users\Ayo Paul\.virtualenvs\python_and_Django_web_development-NQaQovYW\lib\site-packages\django\utils\functional.py" in _setup
347. self._wrapped = self._setupfunc()
File "C:\Users\Ayo Paul\.virtualenvs\python_and_Django_web_development-NQaQovYW\lib\site-packages\django\contrib\auth\middleware.py" in <lambda>
24. request.user = SimpleLazyObject(lambda: get_user(request))
File "C:\Users\Ayo Paul\.virtualenvs\python_and_Django_web_development-NQaQovYW\lib\site-packages\django\contrib\auth\middleware.py" in get_user
12. request._cached_user = auth.get_user(request)
File "C:\Users\Ayo Paul\.virtualenvs\python_and_Django_web_development-NQaQovYW\lib\site-packages\django\contrib\auth\__init__.py" in get_user
182. user_id = _get_user_session_key(request)
File "C:\Users\Ayo Paul\.virtualenvs\python_and_Django_web_development-NQaQovYW\lib\site-packages\django\contrib\auth\__init__.py" in _get_user_session_key
59. return get_user_model()._meta.pk.to_python(request.session[SESSION_KEY])
File "C:\Users\Ayo Paul\.virtualenvs\python_and_Django_web_development-NQaQovYW\lib\site-packages\django\db\models\fields\__init__.py" in to_python
945. params={'value': value},
Exception Type: ValidationError at /admin/
Exception Value: ["'test@user.com' value must be an integer."]
class ClientAuthBackend(object):
def authenticate(self, request, username=None, password=None):
try:
user = User.objects.get(username=username)
if user.check_password(password):
return user
else:
return None
except User.DoesNotExist:
raise ValidationError("Invalid Credentials")
def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None
答案 0 :(得分:-2)
url.simple
删除“ unique = True”。