Subject: [PATCH django-impersonate] Fixing the handling of user models that have a UUID primary key

From: Adam Taylor <[email-masked]>
Date: Mon, 31 Mar 2025 15:13:12 +0000

# HG changeset patch
# User Adam Taylor <[email-masked]>
# Date 1743433368 0
#      Mon Mar 31 15:02:48 2025 +0000
# Node ID b536d2d35e1dac51f35580254f65a7ee30613138
# Parent  33cb8c77262a474869ab94bcb82c5446baf3c228
Fixing the handling of user models that have a UUID primary key

diff --git a/impersonate/views.py b/impersonate/views.py
--- a/impersonate/views.py
+++ b/impersonate/views.py
@@ -1,6 +1,7 @@
 # -*- coding: utf-8 -*-
 import logging
 from datetime import datetime, timezone
+from uuid import UUID
 
 from django.db.models import Q
 from django.http import Http404
@@ -40,7 +41,10 @@
         logger.error(f'views/impersonate: Invalid value for uid given: {uid}')
         raise Http404('Invalid value given.')
     if check_allow_for_user(request, new_user):
-        request.session['_impersonate'] = new_user.pk
+        if isinstance(new_user.pk, UUID):
+            request.session['_impersonate'] = str(new_user.pk)
+        else:
+            request.session['_impersonate'] = new_user.pk
         request.session['_impersonate_start'] = datetime.now(
             tz=timezone.utc
         ).timestamp()