Django UUIDField 模型字段导致 Django 管理错误:格式错误的十六进制 UUID 字符串 [英] Django UUIDField modelfield causes error in Django admin: badly formed hexadecimal UUID string

查看:23
本文介绍了Django UUIDField 模型字段导致 Django 管理错误:格式错误的十六进制 UUID 字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Django 1.8 项目,在我的一个模型上,我正在使用新的 UUIDField,如下所示:

I have a Django 1.8 project and on one of my models, I am using the new UUIDField like so:

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

我还设置了我的 admin.py:

I've also set up my admin.py:

@admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
    pass

当我加载管理页面以尝试创建实例时,出现错误:

When I load the admin page to try to create an instance, I get an error:

ValueError at /admin/core/mymodel/add/
badly formed hexadecimal UUID string

我能够从 Django shell (./manage.py shell) 创建一个没有问题的实例.但是,一旦我这样做了,即使在查看对象实例列表时,我也会在管理站点上遇到与以前相同的错误.

I am able to create an instance no problem from the Django shell (./manage.py shell). Once I've done that though, I get the same error as before on the admin site even when viewing the list of object instances.

有什么想法吗?

推荐答案

问题是我在 DB 中有一个现有记录,它带有默认整数自动增量 id,在我明确指出 id 之前我的模型上的 code>id 字段是 UUIDField.该字段的值仅为 1,它不是有效的 UUID 十六进制字符串.

The problem is that I had an existing record in the DB with a default integer autoincrement id, before I had specific that the id field on my model was a UUIDField. The value of this field was just 1, which was not a valid UUID hex string.

删除此记录解决了我的问题.

Removing this record fixed my issue.

这篇关于Django UUIDField 模型字段导致 Django 管理错误:格式错误的十六进制 UUID 字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆