Django UUIDField modelfield导致Django管理员中的错误:格式错误的十六进制UUID字符串 [英] Django UUIDField modelfield causes error in Django admin: badly formed hexadecimal UUID string
问题描述
class MyModel(models.Model):
id = models.UUIDField(primary_key = True,default = uuid.uuid4,editable = False)
我还设置了我的admin.py:
@ admin.register( MyModel)
class MyModelAdmin(admin.ModelAdmin):
pass
当我加载管理页面尝试创建一个实例,我收到一个错误:
ValueError at / admin / core / mymodel / add /
格式不正确的十六进制UUID字符串
我可以创建一个没有问题的实例Django shell( ./ manage.py shell
)。一旦我完成了这一点,即使在查看对象实例列表时,我也会在管理员网站上获得与以前相同的错误。
任何想法?
问题是我在DB中有一个现有的记录,默认的整数自动增量 id
,在我具体说明我的模型的 id
字段是一个 UUIDField
之前。该字段的值只是 1
,这不是有效的UUID十六进制字符串。
删除此记录修复了我的问题。
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)
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
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.
Any thoughts?
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 modelfield导致Django管理员中的错误:格式错误的十六进制UUID字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!