Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...