UUID('...') is not JSON serializable

There is a bug ticket on Django regarding this issue however a custom so called 'complex encoder' by python docs can help you.

import json
from uuid import UUID


class UUIDEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, UUID):
            # if the obj is uuid, we simply return the value of uuid
            return obj.hex
        return json.JSONEncoder.default(self, obj)

Now if we did something like this

json.dumps(my_object, cls=UUIDEncoder)

Your uuid field should be encoded.


Convert UUID to str.

uuid_str = str(uuid_item)


For using the UUID in a URL like that, you should pass it as a string:

 return redirect(reverse('namespace:name', kwargs={'uuid' : str(object.id)}))

FYI - it looks like WIMs answer is a bit more thorough. Your regex should certainly be tightened up. If you end up using the string representation of the slug, you'll want a regex like this: [A-Za-z0-9\-]+ which allows for alphanumerics and hyphens.