Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#24161 closed Bug (fixed)

Using UUIDField for id of custom User model prevents logging in

Reported by: James Beith Owned by: nobody
Component: contrib.sessions Version: 1.8alpha1
Severity: Release blocker Keywords:
Cc: Simon Charette Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When using the new UUIDField in Django 1.8 for the id of a custom User model, a session fails to be created and a TypeError exception is thrown when attempting to log in.

TypeError at /login/
UUID('314cb5d6-593f-48f5-b1af-fadedde123d3') is not JSON serializable

Please see the README.md file of the attached example project.

Attachments (1)

619295fe.zip (8.7 KB) - added by James Beith 5 years ago.
Example project

Download all attachments as: .zip

Change History (9)

Changed 5 years ago by James Beith

Attachment: 619295fe.zip added

Example project

comment:1 Changed 5 years ago by Simon Charette

Thanks for the example project. Could you also provide the full traceback please.

comment:2 Changed 5 years ago by Simon Charette

Triage Stage: UnreviewedAccepted

I guess we should use user.serializable_value(user._meta.pk.attname) here.

comment:3 in reply to:  1 Changed 5 years ago by James Beith

Replying to charettes:

Thanks for the example project. Could you also provide the full traceback please.

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/login/?next=/

Django Version: 1.8
Python Version: 3.4.2
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'myapp')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware')


Traceback:
File "/Users/James/.virtualenvs/uuid-user/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
  222.                 response = middleware_method(request, response)
File "/Users/James/.virtualenvs/uuid-user/lib/python3.4/site-packages/django/contrib/sessions/middleware.py" in process_response
  49.                         request.session.save()
File "/Users/James/.virtualenvs/uuid-user/lib/python3.4/site-packages/django/contrib/sessions/backends/db.py" in save
  58.             session_data=self.encode(self._get_session(no_load=must_create)),
File "/Users/James/.virtualenvs/uuid-user/lib/python3.4/site-packages/django/contrib/sessions/backends/base.py" in encode
  89.         serialized = self.serializer().dumps(session_dict)
File "/Users/James/.virtualenvs/uuid-user/lib/python3.4/site-packages/django/core/signing.py" in dumps
  90.         return json.dumps(obj, separators=(',', ':')).encode('latin-1')
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/json/__init__.py" in dumps
  237.         **kw).encode(obj)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/json/encoder.py" in encode
  192.         chunks = self.iterencode(o, _one_shot=True)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/json/encoder.py" in iterencode
  250.         return _iterencode(o, 0)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/json/encoder.py" in default
  173.         raise TypeError(repr(o) + " is not JSON serializable")

Exception Type: TypeError at /login/
Exception Value: UUID('314cb5d6-593f-48f5-b1af-fadedde123d3') is not JSON serializable
Last edited 5 years ago by James Beith (previous) (diff)

comment:4 Changed 5 years ago by Simon Charette

Has patch: set

Create a PR but I'm not sure get_prep_value is the correct solution here.

Maybe we should document that your session serializer should be able to deal with your user primary key type?

I guess we could also rely on the check framework to make sure it's the case.

comment:5 Changed 5 years ago by Simon Charette

Cc: Simon Charette added

comment:6 Changed 5 years ago by Simon Charette

Triage Stage: AcceptedReady for checkin

Tim's patch LGTM.

comment:7 Changed 5 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: newclosed

In 0f7f5bc9e7a94ab91c2b3db29ef7cf000eff593f:

Fixed #24161 -- Stored the user primary key as a serialized value in the session.

This allows using a UUIDField primary key along with the JSON session
serializer.

Thanks to Trac alias jamesbeith for the report and Simon Charette
for the initial patch.

comment:8 Changed 5 years ago by Tim Graham <timograham@…>

In 596564e808089f1263469dbf4744dcaa05b79222:

[1.8.x] Fixed #24161 -- Stored the user primary key as a serialized value in the session.

This allows using a UUIDField primary key along with the JSON session
serializer.

Thanks to Trac alias jamesbeith for the report and Simon Charette
for the initial patch.

Backport of 0f7f5bc9e7a94ab91c2b3db29ef7cf000eff593f from master

Note: See TracTickets for help on using tickets.
Back to Top