5

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?

Joseph
  • 12,678
  • 19
  • 76
  • 115
  • Can we see your full model and URLs? What DB are you using? One suggestion is to check the size of the id field in the DB, if you have run migrations before. – Glyn Jackson Sep 07 '15 at 20:27
  • shouldn't it be: default=uuid.uuid4() ? – DevLounge Sep 07 '15 at 22:33
  • @Apero, no, the value for the `default` parameter should be a callable. In this case, `uuid.uuid4` is correct. @GlynJackson - I'm not sure why the rest of the model / URLs matter. I will look into my DB to double check the size is right. I don't expect that to be the problem considering I can create/access instances via the shell without problem. It's the admin interface that is the issue. – Joseph Sep 08 '15 at 16:18

1 Answers1

11

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.

Joseph
  • 12,678
  • 19
  • 76
  • 115