【问题标题】:Django: How to fix UUID ValueError('badly formed hexadecimal UUID string')?Django:如何修复 UUID ValueError('格式错误的十六进制 UUID 字符串')?
【发布时间】:2020-08-06 19:15:28
【问题描述】:

我正在使用 Python 3.8 和 Django 3.0。

模型.py

class CustomeUser(AbstractUser):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
...

class ActivationCode(models.Model):
Code = models.CharField(max_length=30, null=False, unique=True)
ForUser = models.ForeignKey(CustomUser, null=True, blank=True, on_delete=models.CASCADE)
...

代码:

jj = {'app_id': '100',
 'color': 'blue',
 'os_type': 'MacOS',
 'time': '1596397345',
 'regKey': 'dJbeetbQ',
 'os_version': '10.15.6',
 'place_id': '20190228-3',
 'version': '2.0.0.8',
 'guid': '10614ba9b54f909a715ed518cc39741811369a11',
 'randomString': '3808651424'}

regKey = jj["regKey"]
record = ActivationCode.objects.get(Code=regKey)

当 regKey 匹配用户记录时会出现异常。如果ForUser字段为null,则通过。

这里有什么问题?

完整追溯:

Traceback (most recent call last):
  File "C:\Users\Ken\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3331, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-59-b3b355ae1522>", line 1, in <module>
    ActivationCode.objects.get(Code=regKey)
  File "C:\Users\Ken\anaconda3\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\Ken\anaconda3\lib\site-packages\django\db\models\query.py", line 411, in get
    num = len(clone)
  File "C:\Users\Ken\anaconda3\lib\site-packages\django\db\models\query.py", line 258, in __len__
    self._fetch_all()
  File "C:\Users\Ken\anaconda3\lib\site-packages\django\db\models\query.py", line 1261, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "C:\Users\Ken\anaconda3\lib\site-packages\django\db\models\query.py", line 74, in __iter__
    for row in compiler.results_iter(results):
  File "C:\Users\Ken\anaconda3\lib\site-packages\django\db\models\sql\compiler.py", line 1088, in apply_converters
    value = converter(value, expression, connection)
  File "C:\Users\Ken\anaconda3\lib\site-packages\django\db\backends\mysql\operations.py", line 270, in convert_uuidfield_value
    value = uuid.UUID(value)
  File "C:\Users\Ken\anaconda3\lib\uuid.py", line 160, in __init__
    raise ValueError('badly formed hexadecimal UUID string')
ValueError: badly formed hexadecimal UUID string

【问题讨论】:

  • 你导入模块了吗? import uuid
  • 你能发布完整的回溯
  • @Kaushal 是的,我做到了。
  • ForUser_id 列中是否有一些不良数据?此错误可能是由于该列中的值不是有效的 UUID
  • 我有一些记录是空的。但没有坏数据。

标签: python python-3.x django


【解决方案1】:

这个错误的根本原因仅仅是因为您在ForUser_id 列中有一个non-UUID 值。

正如你在 cmets 中所说的

我有一些记录是空的。但没有坏数据。

在我看来,null 似乎是导致此错误的原因。也不建议 UUIDField 使用 null 值,而是使用空字符串值约定。 Using the null and blank field options 上的博客值得一读。

【讨论】:

    【解决方案2】:

    我已经弄清楚出了什么问题。

    问题是由我在 MySQL 服务器中使用 UUID() 更新的一些用户记录引起的。但是 MYSQL 中的 UUID() 是 36 字节,而 Django 中的 UUID 字段是 32 字节。

    现在我又用id = UUID().Replace('-','')更新了id,问题就解决了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-28
      • 2016-05-29
      • 1970-01-01
      • 2016-11-22
      相关资源
      最近更新 更多