Django UUIDField shows 'badly formed hexadecimal UUID string' error?

I have written a model before and used the UUIDField and everything worked perfectly well, now i tried doing exactly the same thing but i get the error badly formed hexadecimal UUID string, i don’t know if i messed something up. I have imported the uuid package and the field goes thus id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False).

I have also tried adding () ..., default.uuid.uuid4() BUT it does not still work, what might be the problem and it’s best fix?

class Channel(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    name = models.CharField(max_length=200) 
    user = models.OneToOneField(User, on_delete=models.SET_NULL, null=True, related_name="channel")
    ... ## There are other field below the ones above

What is the specific situation where you’re getting this error? It would be helpful if you posted the complete traceback.

This is comeplete traceback

PS C:\Users\Destiny\Desktop\Youtube CLone\ytprj> python manage.py makemigrations channel
  channel\migrations\0002_alter_channel_id.py
    - Alter field id on channel
PS C:\Users\Destiny\Desktop\Youtube CLone\ytprj> python manage.py migrate channel
Operations to perform:
Running migrations:
  No migrations to apply.
PS C:\Users\Destiny\Desktop\Youtube CLone\ytprj> python manage.py migrate channel
Operations to perform:
Running migrations:
  No migrations to apply.
PS C:\Users\Destiny\Desktop\Youtube CLone\ytprj> python manage.py migrate
Operations to perform:
Running migrations:
  No migrations to apply.
PS C:\Users\Destiny\Desktop\Youtube CLone\ytprj> python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
September 10, 2022 - 15:15:41
Django version 3.2.7, using settings 'ytprj.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Internal Server Error: /admin/channel/channel/
Traceback (most recent call last):
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\admin\options.py", line 616, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\decorators.py", line 130, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\views\decorators\cache.py", line 44, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\admin\sites.py", line 232, in inner
    return view(request, *args, **kwargs)
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\import_export\admin.py", line 308, in changelist_view
    return super().changelist_view(request, extra_context)
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\import_export\admin.py", line 446, in changelist_view
    return super().changelist_view(request, extra_context)
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\decorators.py", line 43, in _wrapper
    return bound_method(*args, **kwargs)
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\decorators.py", line 130, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\admin\options.py", line 1815, in changelist_view
    'selection_note': _('0 of %(cnt)s selected') % {'cnt': len(cl.result_list)},
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\query.py", line 262, in __len__
    self._fetch_all()
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\query.py", line 1324, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\query.py", line 68, in __iter__
    for row in compiler.results_iter(results):
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\sql\compiler.py", line 1122, in apply_converters
    value = converter(value, expression, connection)
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\sqlite3\operations.py", 
line 322, in convert_uuidfield_value
    value = uuid.UUID(value)
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\uuid.py", line 177, in __init__
    raise ValueError('badly formed hexadecimal UUID string')
ValueError: badly formed hexadecimal UUID string
[10/Sep/2022 15:15:43] "GET /admin/channel/channel/ HTTP/1.1" 500 140676

It looks like you changed your existing id field from the auto field to this UUID field - that’s not going to work for existing data. Changing the field type does not cause the data in that field to be changed by the migration.

You’ll probably need to undo this migration, and (possibly) migrate this model to a different model to cleanly accomplish this.

You might be able to make this work by adding a new (non-primary key) field for the UUID, create UUIDs in that field, then change it to become the new primary key field.

Either way, this isn’t the type of change that can be made directly with pre-existing data.

Okay, i just did that and it seem not to be showing the error badly formed hexadecimal UUID string again, but when i fill the form and hit the save button from the admin section
it then show this OverflowError Python int too large to convert to SQLite INTEGER.

You’ll need to post your code (current model and view) for us to be able to offer any suggestions.

1 Like

views.py


def channel_profile(request, channel_name):
    channel = get_object_or_404(Channel, id=channel_name)
    context = {
        "channel": channel,
    }
    return render(request, "channel/channel.html", context)

models.py


class Channel(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=True)
    full_name = models.CharField(max_length=200) 
    channel_name = models.CharField(max_length=200) 
    user = models.OneToOneField(User, on_delete=models.SET_NULL, null=True, related_name="channel")
    ...
    def __str__(self):
        return self.channel_name
    

These are my models.py and views.py

This makes me think that the underlying table still has the id column as an integer field instead of a char(32), which is how a uuid field is stored in sqlite. Check the structure of the table in the database and verify that the migrations you have performed would have altered the datatype of that column.