Django UUIDField modelfield causes error in Django admin: badly formed hexadecimal UUID string

↘锁芯ラ 提交于 2019-12-29 08:46:11

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/32445546/django-uuidfield-modelfield-causes-error-in-django-admin-badly-formed-hexadecim

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!