You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django 5.0.4创建超级用户时触发UUID格式错误ValueError

问题:Django 5.0.4创建超级用户时触发UUID格式错误

执行createsuperuser命令时出现如下错误:

ValueError: badly formed hexadecimal UUID string

堆栈跟踪信息

File "/home/ubuntu/my_api/manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/home/ubuntu/myenv/lib/python3.10/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/home/ubuntu/myenv/lib/python3.10/site-packages/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/ubuntu/myenv/lib/python3.10/site-packages/django/core/management/base.py", line 413, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/ubuntu/myenv/lib/python3.10/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 89, in execute
    return super().execute(*args, **options)
  File "/home/ubuntu/myenv/lib/python3.10/site-packages/django/core/management/base.py", line 459, in execute
    output = self.handle(*args, **options)
  File "/home/ubuntu/myenv/lib/python3.10/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 131, in handle
    error_msg = self._validate_username(
  File "/home/ubuntu/myenv/lib/python3.10/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 304, in _validate_username
    self.UserModel._default_manager.db_manager(database).get_by_natural_key(
  File "/home/ubuntu/my_api/my_auth/models.py", line 55, in get_by_natural_key
    uuid = UUID(uuid)
  File "/usr/lib/python3.10/uuid.py", line 177, in __init__
    raise ValueError('badly formed hexadecimal UUID string')
ValueError: badly formed hexadecimal UUID string

自定义User模型及管理器代码

from uuid import UUID

class UserManager(DjangoUserManager):
    def get_by_natural_key(self, uuid):
        if not isinstance(uuid, UUID):
            uuid = UUID(uuid)

        return self.get(uuid=uuid)


class User(AbstractUser):
    """
    Custom user model to change behaviour of the default user model
    such as validation and required fields.
    """
    uuid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True,verbose_name=_("UUID"))

已尝试但无效的操作

  • 删除迁移文件
  • 使用flush命令清空数据库
  • 使用reset_db命令重置数据库

解决方案

问题根源

createsuperuser命令在验证用户名唯一性时,会调用get_by_natural_key方法,传入的参数是用户输入的用户名(比如admin),但你重写的get_by_natural_key错误地将其当作UUID字符串解析,导致格式错误。

Django默认的AbstractUser以username作为natural key,你的重写逻辑破坏了这一默认行为。

修复代码

修改UserManager的get_by_natural_key方法,恢复默认按用户名查询的逻辑:

class UserManager(DjangoUserManager):
    def get_by_natural_key(self, username):
        # 按用户名查询,适配createsuperuser的默认验证流程
        return self.get(username=username)

后续操作

  1. 保存修改后的模型代码
  2. 重新执行数据库迁移(如果有必要):
python manage.py makemigrations
python manage.py migrate
  1. 再次运行createsuperuser命令即可正常创建超级用户。

内容的提问来源于stack exchange,提问作者Ananth

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 05:03:23