请问,我如何解决管理员的验证错误(“”值必须是整数)

Posted

技术标签:

【中文标题】请问,我如何解决管理员的验证错误(“”值必须是整数)【英文标题】:Please, how do i resolve the validation error at admin("" value must be an integer) 【发布时间】:2019-06-02 01:46:48 【问题描述】:

每当我启动服务器并尝试访问管理页面时,我经常会收到 ValidationError: ['test@user.com' value must be an integer]。

我已经为项目创建了自定义用户,并按照文档中的说明完成了必要的设置(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

【问题讨论】:

user_id 字段中的内容是什么? 请问我不明白这个问题,请 我的怀疑是您将电子邮件存储在 user_id 字段中,您可以查询您的数据库以查看 user_id 字段的内容吗? 问题是存储在request.session[SESSION_KEY] 中的内容。你是怎么设置的?您有自定义身份验证后端吗? 请看一下自定义身份验证后端。谢谢 【参考方案1】:
user_id = models.AutoField(primary_key=True)

删除“unique = True”。

【讨论】:

这个答案完全没用 - unique arg 虽然对于主键(根据定义是唯一的)无用,但不会更改列的类型。

以上是关于请问,我如何解决管理员的验证错误(“”值必须是整数)的主要内容,如果未能解决你的问题,请参考以下文章

错误列表索引必须是整数或切片,而不是str

outlook 2013 无法验证您连接的服务器的安全证书 请问这个问题您是怎么解决的?

我用sql server 2008 management studio 连接 sql server 2005, 提示4064错误,请问如何解决?

如何使用正则表达式验证范围 1-99?

网上申报地税,打开网页显示java.lang.NullPointerException这个,请问如何解决?

如何使用身份验证管理器修复“错误凭据”错误?