最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Django添加用户出错:django.db.utils.IntegrityError 1062 Duplicate entry for key mobile_phone_number

Django crifan 5118浏览 0评论

折腾:

【已解决】给Django系统中添加一个新的用户

期间,尝试通过django的后台管理页面中去新建用户:

http://localhost:65000/admin/user/user/add/

结果出错:

django.db.utils.IntegrityError

django.db.utils.IntegrityError: (1062, "Duplicate entry ” for key ‘mobile_phone_number’")

Traceback (most recent call last)

File "/usr/local/lib/python3.6/site-packages/django/contrib/staticfiles/handlers.py", line 66, in __call__

return self.application(environ, start_response)

File "/usr/local/lib/python3.6/site-packages/django/core/handlers/wsgi.py", line 146, in __call__

response = self.get_response(request)

File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 81, in get_response

File "/usr/local/lib/python3.6/site-packages/MySQLdb/cursors.py", line 247, in execute

res = self._query(query)

File "/usr/local/lib/python3.6/site-packages/MySQLdb/cursors.py", line 411, in _query

rowcount = self._do_query(q)

File "/usr/local/lib/python3.6/site-packages/MySQLdb/cursors.py", line 374, in _do_query

db.query(q)

File "/usr/local/lib/python3.6/site-packages/MySQLdb/connections.py", line 277, in query

_mysql.connection.query(self, query)

django.db.utils.IntegrityError: (1062, "Duplicate entry ” for key ‘mobile_phone_number’")

The debugger caught an exception in your WSGI application. You can now look at the traceback which led to the error.

To switch between the interactive traceback and the plaintext one, you can click on the "Traceback" headline. From the text traceback you can also create a paste of it. For code execution mouse-over the frame you want to debug and click on the console icon on the right side.

You can execute arbitrary Python code in the stack frames and there are some extra helpers available for introspection:

* dump() shows all variables in the frame

* dump(obj) dumps all that’s known about the object

Brought to you by DON’T PANIC, your friendly Werkzeug powered traceback interpreter.

看错误信息像是:

mobile_phone_number不能重复

而之前有个账号的mobile_phone_number是空字符串

此处新建账号,mobile_phone_number是空(因为没有输入的地方)

导致mysql报重复的错误,创建失败

而去看数据库,果然符合猜测:

CREATE TABLE `user_user` (

  `password` varchar(128) NOT NULL,

  `last_login` datetime(6) DEFAULT NULL,

  `is_superuser` tinyint(1) NOT NULL,

  `username` varchar(150) NOT NULL,

  `first_name` varchar(30) NOT NULL,

  `last_name` varchar(150) NOT NULL,

  `email` varchar(254) NOT NULL,

  `is_staff` tinyint(1) NOT NULL,

  `date_joined` datetime(6) NOT NULL,

  `id` char(32) NOT NULL,

  `is_active` tinyint(1) NOT NULL,

  `name` varchar(128) NOT NULL,

  `mobile_phone_number` varchar(11) NOT NULL,

  PRIMARY KEY (`id`),

  UNIQUE KEY `username` (`username`),

  UNIQUE KEY `mobile_phone_number` (`mobile_phone_number`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

去看数据,果然有个用户(此处的超级管理员)的phones是空的:

Django 创建/删除用户 – CSDN博客

Django 创建超级用户 – CSDN博客

django官方文档——django中的用户认证 – CSDN博客

试试 保存并继续编辑

错误依旧

django.db.utils.IntegrityError

django.db.utils.IntegrityError: (1062, "Duplicate entry ” for key ‘mobile_phone_number’")

好像可以用代码去创建:

django官方文档——django中的用户认证 – CSDN博客

>>> from django.contrib.auth.models import User

>>> user = User.objects.create_user(‘john’, ‘[email protected]‘, ‘johnpassword’)

待会试试

Django用户认证系统(一)User对象 – 再见紫罗兰 – 博客园

[django]让后台新增用户的表单包含 email 字段 – Huang Huang 的博客

像是我需要的

Extending new user form, in the admin Django – Stack Overflow

不过帖子够老是2011年的

Django 用户认证系统:拓展 User 模型 – 掘金

【总结】

对于前面错误,貌似可以直接去修改数据库,然后把之前手机号为空的,补上

估计新建就可以了,然后再去更新补全相关信息,包括手机号即可。

但是还是不够智能,还是希望能在管理页面上能够输入这些信息

-》确保以后也方便通过页面上去新增用户,且填写对应信息,所以去:

【已解决】Django后台管理页面中给新增用户添加自定义字段

转载请注明:在路上 » 【已解决】Django添加用户出错:django.db.utils.IntegrityError 1062 Duplicate entry for key mobile_phone_number

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
88 queries in 0.180 seconds, using 22.16MB memory