0

I am using Django==1.9 , My models.py looks something as

from __future__ import unicode_literals

from django.db import models
import uuid

# Create your models here.
class Status(models.Model):
    id = models.UUIDField(default=uuid.uuid4, editable=False)
    comment = models.CharField(max_length=1000, unique=True)

When I run python manage.py migrate it throws me

  File "C:\Python27\Lib\uuid.py", line 136, in __init__
    raise ValueError('badly formed hexadecimal UUID string')
ValueError: badly formed hexadecimal UUID string

Note that, The database table is not yet created in sqlite3.db so other answers for the similar looking question on Stack-Overflow suggests you should drop the database if exists and then migrate. But in my case the database doesn't exists as of now.

I tried most of the Stack-Overflow answer, some suggest use default='' , some says default=uuid.uuid4() etc. Nothing works for me, that is the reason why I created a new question. So, please don't link to answer just by blindly looking at the title !

The answers which I already referred to are as below: Python: How to solve the issue : 'badly formed hexadecimal UUID string' in Django

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

Marcus
  • 105
  • 2
  • 11
  • If the answers say to use `default=uuid.uuid4()`, why are you using `default=uuid.uuid4`? There's a big difference between the function object, and the UUID object that you get from calling the function. (Although it seems unlikely to me that `default=uuid.uuid4()` really would be correct, because this would just give the same UUID to everyone…) – abarnert Jun 13 '18 at 21:11
  • Looking at [the docs](https://docs.djangoproject.com/en/1.9/ref/models/fields/#uuidfield), `uuid.uuid4` is not only correct, it's the default default if you don't specify one. So, unless your Django installation is completely broken, I suspect you're doing something wrong somewhere else. Can you give us a [mcve] that reproduces the problem? – abarnert Jun 13 '18 at 21:15
  • @abarnert : If you use `uuid.uuid4()` then it does not works. Even the `makemigrations` fails in that case – Marcus Jun 13 '18 at 21:17
  • The only reason I even mentioned it is that your question says that lots of Stack Overflow answers say to do that. I even said it seemed unlikely that was true. But you're the one who said it, and nobody else but you can verify it, since you didn't tell us which answers or give us any links. – abarnert Jun 13 '18 at 21:20

0 Answers0