Member-only story
In our previous series we covered a way to replace a module in the runtime. Here we are going to use it and replace the default python uuid
module with a more faster implementation uuid_utils
https://pypi.org/project/uuid-utils.
Let’s install the fast uuid library pip install uuid_utils
and without covering the details let’s present the code which is going to replace the uuid:
import sys
import uuid
import uuid_utils
from psycopg2.extensions import register_adapter
from psycopg2.extras import UUID_adapter
def replace_uuid_module():
replacement_module = sys.modules.get("uuid_utils")
original_module = sys.modules.get("uuid")
replacement_module._load_system_functions = lambda: None
replacement_module._generate_time_safe = None
sys.modules["uuid"] = replacement_module
register_adapter(uuid_utils.UUID, UUID_adapter)
To make Django ORM work we need to specify the type adapter for uuid_utils.UUID
class (and it can be the regular UUID_adapter
since uuid from uuid_utils
is backward compatible). You may skip setting _load_system_functions
and _generate_time_safe
to the shown values, since it can work without it.
Replacing in the Test Run
In django we run test by the following command python manage.py test
thus by modifying manage.py
…