sehmaschine / django-grappelli

A jazzy skin for the Django Admin-Interface (official repository).
http://www.grappelliproject.com
Other
3.77k stars 657 forks source link

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

Closed elnygren closed 1 year ago

elnygren commented 1 year ago
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 commented 1 year ago

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

https://github.com/sehmaschine/django-grappelli/blob/3fd18d73544433e5c9d3a89bdbe61dc07c4c518f/grappelli/views/switch.py#L73

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 commented 1 year ago

https://docs.djangoproject.com/en/4.2/ref/settings/#session-serializer

sehmaschine commented 11 months ago

@elnygren Why did you close this?

elnygren commented 8 months ago

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