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

**URL:** <https://forum.djangoproject.com/t/django-uuidfield-shows-badly-formed-hexadecimal-uuid-string-error/15916>\
**Category:** Using the ORM\
**Created:** [September 10, 2022, 2:02pm UTC](https://forum.djangoproject.com/t/django-uuidfield-shows-badly-formed-hexadecimal-uuid-string-error/15916 "2022-09-10T14:02:56Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![desphixs](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/desphixs/32/6411_2.png) [@desphixs](https://forum.djangoproject.com/u/desphixs)\
**Post date:** [September 10, 2022, 2:02pm UTC](https://forum.djangoproject.com/t/django-uuidfield-shows-badly-formed-hexadecimal-uuid-string-error/15916/1 "2022-09-10T14:02:56Z")

</div>

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?

```auto
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

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [September 10, 2022, 2:12pm UTC](https://forum.djangoproject.com/t/django-uuidfield-shows-badly-formed-hexadecimal-uuid-string-error/15916/2 "2022-09-10T14:12:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![desphixs](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/desphixs/32/6411_2.png) [@desphixs](https://forum.djangoproject.com/u/desphixs)\
**Post date:** [September 10, 2022, 2:16pm UTC](https://forum.djangoproject.com/t/django-uuidfield-shows-badly-formed-hexadecimal-uuid-string-error/15916/3 "2022-09-10T14:16:34Z")

</div>

This is comeplete traceback

```auto
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

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [September 10, 2022, 3:53pm UTC](https://forum.djangoproject.com/t/django-uuidfield-shows-badly-formed-hexadecimal-uuid-string-error/15916/4 "2022-09-10T15:53:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![desphixs](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/desphixs/32/6411_2.png) [@desphixs](https://forum.djangoproject.com/u/desphixs)\
**Post date:** [September 10, 2022, 4:10pm UTC](https://forum.djangoproject.com/t/django-uuidfield-shows-badly-formed-hexadecimal-uuid-string-error/15916/5 "2022-09-10T16:10:27Z")

</div>

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`.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [September 10, 2022, 4:12pm UTC](https://forum.djangoproject.com/t/django-uuidfield-shows-badly-formed-hexadecimal-uuid-string-error/15916/6 "2022-09-10T16:12:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![desphixs](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/desphixs/32/6411_2.png) [@desphixs](https://forum.djangoproject.com/u/desphixs)\
**Post date:** [September 10, 2022, 4:29pm UTC](https://forum.djangoproject.com/t/django-uuidfield-shows-badly-formed-hexadecimal-uuid-string-error/15916/7 "2022-09-10T16:29:22Z")

</div>

views.py

```auto

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

```auto

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**

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [September 10, 2022, 6:44pm UTC](https://forum.djangoproject.com/t/django-uuidfield-shows-badly-formed-hexadecimal-uuid-string-error/15916/8 "2022-09-10T18:44:01Z")

</div>

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.
