ValueError: badly formed hexadecimal UUID string
ValueError: badly formed hexadecimal UUID string
在我的系统中,我试图为每个用户自动生成一个唯一的 运行dom 字符串,稍后在 URL 中作为该帐户的引用传递,我最近阅读了Django 模型中的 UUID。这就是我的模型的样子:
class User(AbstractBaseUser, PermissionsMixin):
card_number = models.CharField(max_length=20, unique=True)
first_name = models.CharField(max_length=150)
middle_name = models.CharField(max_length=150)
last_name = models.CharField(max_length=150)
address = models.CharField(max_length=200)
account_code = models.UUIDField(max_length=200, default=uuid.uuid4(), editable=False)
我 运行 在终端中开始创建超级用户时遇到错误:
Traceback (most recent call last):
File "C:\Users\User\PycharmProjects\ViajeroProject\viaje\manage.py", line 22, in <module>
main()
File "C:\Users\User\PycharmProjects\ViajeroProject\viaje\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\User\PycharmProjects\ViajeroProject\venv\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "C:\Users\User\PycharmProjects\ViajeroProject\venv\lib\site-packages\django\core\management\__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\User\PycharmProjects\ViajeroProject\venv\lib\site-packages\django\core\management\base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\User\PycharmProjects\ViajeroProject\venv\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 79, in execute
return super().execute(*args, **options)
File "C:\Users\User\PycharmProjects\ViajeroProject\venv\lib\site-packages\django\core\management\base.py", line 371, in execute
output = self.handle(*args, **options)
File "C:\Users\User\PycharmProjects\ViajeroProject\venv\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 113, in handle
error_msg = self._validate_username(username, verbose_field_name, database)
File "C:\Users\User\PycharmProjects\ViajeroProject\venv\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 234, in _validate_username
self.UserModel._default_manager.db_manager(database).get_by_natural_key(username)
File "C:\Users\User\PycharmProjects\ViajeroProject\venv\lib\site-packages\django\contrib\auth\base_user.py", line 45, in get_by_natural_key
return self.get(**{self.model.USERNAME_FIELD: username})
File "C:\Users\User\PycharmProjects\ViajeroProject\venv\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\User\PycharmProjects\ViajeroProject\venv\lib\site-packages\django\db\models\query.py", line 425, in get
num = len(clone)
File "C:\Users\User\PycharmProjects\ViajeroProject\venv\lib\site-packages\django\db\models\query.py", line 269, in __len__
self._fetch_all()
File "C:\Users\User\PycharmProjects\ViajeroProject\venv\lib\site-packages\django\db\models\query.py", line 1308, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "C:\Users\User\PycharmProjects\ViajeroProject\venv\lib\site-packages\django\db\models\query.py", line 70, in __iter__
for row in compiler.results_iter(results):
File "C:\Users\User\PycharmProjects\ViajeroProject\venv\lib\site-packages\django\db\models\sql\compiler.py", line 1100, in apply_converters
value = converter(value, expression, connection)
File "C:\Users\User\PycharmProjects\ViajeroProject\venv\lib\site-packages\django\db\backends\sqlite3\operations.py", line 318, in convert_uuidfield_value
value = uuid.UUID(value)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\uuid.py", line 177, in __init__
raise ValueError('badly formed hexadecimal UUID string')
ValueError: badly formed hexadecimal UUID string
我应该如何解决这个问题,或者在我自动生成一个唯一的 运行dom 字符串作为 URL 中用户参考的情况下,这种做法是不可取的吗?请注意,用户仍然拥有不同于 account_code.
的 PK
谢谢!
您需要导入 模块 并使用 default=uuid.uuid4
而不是 default=uuid.uuid4()
在我的系统中,我试图为每个用户自动生成一个唯一的 运行dom 字符串,稍后在 URL 中作为该帐户的引用传递,我最近阅读了Django 模型中的 UUID。这就是我的模型的样子:
class User(AbstractBaseUser, PermissionsMixin):
card_number = models.CharField(max_length=20, unique=True)
first_name = models.CharField(max_length=150)
middle_name = models.CharField(max_length=150)
last_name = models.CharField(max_length=150)
address = models.CharField(max_length=200)
account_code = models.UUIDField(max_length=200, default=uuid.uuid4(), editable=False)
我 运行 在终端中开始创建超级用户时遇到错误:
Traceback (most recent call last):
File "C:\Users\User\PycharmProjects\ViajeroProject\viaje\manage.py", line 22, in <module>
main()
File "C:\Users\User\PycharmProjects\ViajeroProject\viaje\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\User\PycharmProjects\ViajeroProject\venv\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "C:\Users\User\PycharmProjects\ViajeroProject\venv\lib\site-packages\django\core\management\__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\User\PycharmProjects\ViajeroProject\venv\lib\site-packages\django\core\management\base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\User\PycharmProjects\ViajeroProject\venv\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 79, in execute
return super().execute(*args, **options)
File "C:\Users\User\PycharmProjects\ViajeroProject\venv\lib\site-packages\django\core\management\base.py", line 371, in execute
output = self.handle(*args, **options)
File "C:\Users\User\PycharmProjects\ViajeroProject\venv\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 113, in handle
error_msg = self._validate_username(username, verbose_field_name, database)
File "C:\Users\User\PycharmProjects\ViajeroProject\venv\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 234, in _validate_username
self.UserModel._default_manager.db_manager(database).get_by_natural_key(username)
File "C:\Users\User\PycharmProjects\ViajeroProject\venv\lib\site-packages\django\contrib\auth\base_user.py", line 45, in get_by_natural_key
return self.get(**{self.model.USERNAME_FIELD: username})
File "C:\Users\User\PycharmProjects\ViajeroProject\venv\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\User\PycharmProjects\ViajeroProject\venv\lib\site-packages\django\db\models\query.py", line 425, in get
num = len(clone)
File "C:\Users\User\PycharmProjects\ViajeroProject\venv\lib\site-packages\django\db\models\query.py", line 269, in __len__
self._fetch_all()
File "C:\Users\User\PycharmProjects\ViajeroProject\venv\lib\site-packages\django\db\models\query.py", line 1308, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "C:\Users\User\PycharmProjects\ViajeroProject\venv\lib\site-packages\django\db\models\query.py", line 70, in __iter__
for row in compiler.results_iter(results):
File "C:\Users\User\PycharmProjects\ViajeroProject\venv\lib\site-packages\django\db\models\sql\compiler.py", line 1100, in apply_converters
value = converter(value, expression, connection)
File "C:\Users\User\PycharmProjects\ViajeroProject\venv\lib\site-packages\django\db\backends\sqlite3\operations.py", line 318, in convert_uuidfield_value
value = uuid.UUID(value)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\uuid.py", line 177, in __init__
raise ValueError('badly formed hexadecimal UUID string')
ValueError: badly formed hexadecimal UUID string
我应该如何解决这个问题,或者在我自动生成一个唯一的 运行dom 字符串作为 URL 中用户参考的情况下,这种做法是不可取的吗?请注意,用户仍然拥有不同于 account_code.
的 PK谢谢!
您需要导入 模块 并使用 default=uuid.uuid4
而不是 default=uuid.uuid4()