python后台创建管理员的时候报错

这里报错的是:

django.db.utils.IntegrityError: (1062, "Duplicate entry '' for key 'mobile'")

翻译过来就是,重复输入了手机号。

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/home/python/.virtualenvs/django_py3_1.11/lib/python3.5/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/home/python/.virtualenvs/django_py3_1.11/lib/python3.5/site-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/python/.virtualenvs/django_py3_1.11/lib/python3.5/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/python/.virtualenvs/django_py3_1.11/lib/python3.5/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 63, in execute
    return super(Command, self).execute(*args, **options)
  File "/home/python/.virtualenvs/django_py3_1.11/lib/python3.5/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/home/python/.virtualenvs/django_py3_1.11/lib/python3.5/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 183, in handle
    self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)
  File "/home/python/.virtualenvs/django_py3_1.11/lib/python3.5/site-packages/django/contrib/auth/models.py", line 170, in create_superuser
    return self._create_user(username, email, password, **extra_fields)
  File "/home/python/.virtualenvs/django_py3_1.11/lib/python3.5/site-packages/django/contrib/auth/models.py", line 153, in _create_user
    user.save(using=self._db)
  File "/home/python/.virtualenvs/django_py3_1.11/lib/python3.5/site-packages/django/contrib/auth/base_user.py", line 80, in save
    super(AbstractBaseUser, self).save(*args, **kwargs)
  File "/home/python/.virtualenvs/django_py3_1.11/lib/python3.5/site-packages/django/db/models/base.py", line 808, in save
    force_update=force_update, update_fields=update_fields)
  File "/home/python/.virtualenvs/django_py3_1.11/lib/python3.5/site-packages/django/db/models/base.py", line 838, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/home/python/.virtualenvs/django_py3_1.11/lib/python3.5/site-packages/django/db/models/base.py", line 924, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "/home/python/.virtualenvs/django_py3_1.11/lib/python3.5/site-packages/django/db/models/base.py", line 963, in _do_insert
    using=using, raw=raw)
  File "/home/python/.virtualenvs/django_py3_1.11/lib/python3.5/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/python/.virtualenvs/django_py3_1.11/lib/python3.5/site-packages/django/db/models/query.py", line 1076, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/home/python/.virtualenvs/django_py3_1.11/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 1112, in execute_sql
    cursor.execute(sql, params)
  File "/home/python/.virtualenvs/django_py3_1.11/lib/python3.5/site-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/home/python/.virtualenvs/django_py3_1.11/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/home/python/.virtualenvs/django_py3_1.11/lib/python3.5/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/python/.virtualenvs/django_py3_1.11/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/home/python/.virtualenvs/django_py3_1.11/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/home/python/.virtualenvs/django_py3_1.11/lib/python3.5/site-packages/django/db/backends/mysql/base.py", line 101, in execute
    return self.cursor.execute(query, args)
  File "/home/python/.virtualenvs/django_py3_1.11/lib/python3.5/site-packages/pymysql/cursors.py", line 170, in execute
    result = self._query(query)
  File "/home/python/.virtualenvs/django_py3_1.11/lib/python3.5/site-packages/pymysql/cursors.py", line 328, in _query
    conn.query(q)
  File "/home/python/.virtualenvs/django_py3_1.11/lib/python3.5/site-packages/pymysql/connections.py", line 516, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "/home/python/.virtualenvs/django_py3_1.11/lib/python3.5/site-packages/pymysql/connections.py", line 727, in _read_query_result
    result.read()
  File "/home/python/.virtualenvs/django_py3_1.11/lib/python3.5/site-packages/pymysql/connections.py", line 1066, in read
    first_packet = self.connection._read_packet()
  File "/home/python/.virtualenvs/django_py3_1.11/lib/python3.5/site-packages/pymysql/connections.py", line 683, in _read_packet
    packet.check_error()
  File "/home/python/.virtualenvs/django_py3_1.11/lib/python3.5/site-packages/pymysql/protocol.py", line 220, in check_error
    err.raise_mysql_exception(self._data)
  File "/home/python/.virtualenvs/django_py3_1.11/lib/python3.5/site-packages/pymysql/err.py", line 109, in raise_mysql_exception
    raise errorclass(errno, errval)
pymysql.err.IntegrityError: (1062, "Duplicate entry '' for key 'mobile'")

分析原因:

因为我们第一次创建管理员的时候,默认不设置手机号,但是我们创建模型类的时候加入了mobile这个字段,所以,当你第二次创建管理员的时候,两个空值的手机冲突了,因为你代码设置了一个手机号只能绑定一个账号,所以,在这里就报错了!

解决办法:

1:删除第一个管理员(不建议,有时候不切实际)

2:给第一个管理员账号加入手机号码

update tb_users set mobile = 'xxxxxxxxxxxx' where id = 1;

总结:就一小问题,稳住
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值