Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch User does not work if User's Primary Key is an UUID #1036

Closed
elnygren opened this issue Nov 15, 2023 · 4 comments
Closed

Switch User does not work if User's Primary Key is an UUID #1036

elnygren opened this issue Nov 15, 2023 · 4 comments

Comments

@elnygren
Copy link

Reverse for 'grp_switch_user' with arguments '(UUID('018aadff-27a6-471d-95aa-783d855c3237'),)' not found. 1 pattern(s) tried: ['grappelli/switch/user/(?P<object_id>\\d+)/$']

Custom User model with is_staff and is_superuser both implemented. Primary Key is an UUIDField. Grappelli 3.0.8 and latest Django.

@elnygren
Copy link
Author

elnygren commented Nov 15, 2023

More info:

GRAPPELLI_SWITCH_USER_REGEX = "[0-9a-f-]+" in settings.py helps to get past the initial problem.

However, changing users will still break because user.id is of type UUID and is not JSON Serializable:

TypeError at /grappelli/switch/user/018aadff-27a6-471d-95aa-783d855c3237/
Object of type UUID is not JSON serializable

The problem is likely in the following row as setting original_user.id in Session won't work

request.session["original_user"] = {"id": original_user.id, "username": original_user.get_username()}

Because Django does not support signing UUIDs (unless you cast to str first) in session:

https://github.com/django/django/blob/36ed45d27cb97fe3a41eca219ba75ff69f16b93c/django/core/signing.py#L118

@elnygren
Copy link
Author

@sehmaschine
Copy link
Owner

@elnygren Why did you close this?

@elnygren
Copy link
Author

elnygren commented Mar 19, 2024

@sehmaschine because session serializer can be used to fix this issue: just make it (de)serialize ULID/UUID properly with a proper JSONEncoder

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants