~petersanchez/django-impersonate#44: 
Accept UUID for User pk

Since Django 1.8 it is possible to define UUID fields, even for primary keys.

Doing the following change possibly is enough?

# from
url(r'^(?P<uid>\d+)/$', 'impersonate', name='impersonate-start'),

# to
url(r'^(?P<uid>[0-9A-Fa-f-]+)/$', 'impersonate', name='impersonate-start'),

To circumvent this issue, I added the following URL resolver before the line includes impersonate.urls:

url(r'^imp_uuid/(?P<uid>[0-9A-Fa-f-]+)$', 'impersonate.views.impersonate',
    name='impersonate-start-uuid'),
url(r'^imp/', include('impersonate.urls')),
Status
REPORTED
Submitter
bitbucket:Eric K.
Assigned to
No-one
Submitted
8 years ago
Updated
2 days ago
Labels
No labels applied.

bitbucket:Eric K. 8 years ago · edit

No Message

bitbucket:Peter Sanchez 8 years ago · edit

Thank you! I'll include this in 1.2. I think it's a simple regex change.

bitbucket:Christopher Bailey 5 years ago · edit

This is still not resolved if you are using a Session backend that is JSON based (like redis). UUIDs are not JSON serializable.

If the pk is a UUID, it should be cast to a str before being stored on the session as this still works:

u = User.objects.get(pk="ffffbc6b-0703-48af-8f76-c418ec608933")

Adam Taylor 8 days ago · edit

I ran into this issue today. My user model has a UUIDField for its primary key. The fix is to edit the views.py file and change this:

request.session['_impersonate'] = new_user.pk

...to this:

request.session['_impersonate'] = str(new_user.pk)

Do you have time to make this change and release a new version?

~petersanchez 4 days ago

Hi Adam,

Did you test this with different session backends? I think that was the hold up reviewing the comments. I haven't had time to even touch this project in a while so if you've got some time to test this and submit a patch that would be greatly appreciated.

See the comment above from Christopher Baily for details.

Adam Taylor 3 days ago · edit

My understanding is that the SESSION_ENGINE setting doesn't affect this issue; it's the SESSION_SERIALIZER setting. The only session serializer that comes with Django is django.contrib.sessions.serializers.JSONSerializer and the documentation says "we highly recommend sticking with JSON serialization". That being the case, I think the best approach is to use isinstance(new_user.pk, uuid.UUID) to determine if the primary key should be cast to a string or not. I would submit a patch, but I don't have an account and it says that registration is closed.

~petersanchez 2 days ago

You don't need an account to submit a patch. See:

https://hg.code.netlandish.com/~petersanchez/django-impersonate#contributing

Patches are sent via hg email to the public mailing list here:

https://lists.code.netlandish.com/~petersanchez/public-inbox

Thank you! I appreciate the help!

~Adam Taylor, Mar 28, 2025 at 20:00:

My understanding is that the SESSION_ENGINE setting doesn't affect this issue; it's the SESSION_SERIALIZER setting. The only session serializer that comes with Django is django.contrib.sessions.serializers.JSONSerializer and the documentation says "we highly recommend sticking with JSON serialization". That being the case, I think the best approach is to use isinstance(new_user.pk, uuid.UUID) to determine if the primary key should be cast to a string or not. I would submit a patch, but I don't have an account and it says that registration is closed.

-- View on the web: https://todo.code.netlandish.com/~petersanchez/django-impersonate/44#event-1240

~petersanchez referenced this from #44 2 days ago

Register here or Log in to comment, or comment via email.