Opened 7 years ago

Last modified 6 years ago

#28575 closed Bug

Model.DoesNotExist classes are not pickleable — at Initial Version

Reported by: Rachel Tobin Owned by: nobody
Component: Database layer (models, ORM) Version: 1.11
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Example:

>>> import django
>>> import pickle
>>> pickle.dumps(django.contrib.auth.models.User.DoesNotExist)
>>> PicklingError: Can't pickle <class 'django.contrib.auth.models.DoesNotExist'>: it's not found as django.contrib.auth.models.DoesNotExist

Note that *instances* of Model.DoesNotExist are pickleable as fixed here: https://code.djangoproject.com/ticket/17776. However the class itself is not. This is particularly problematic when using the Django test runner with the --parallel option enabled when a Model.DoesNotExist exception is raised. The result of this is the test runner bailing with a pickle.PicklingError, making it exceptionally difficult to debug the cause of the Model.DoesNotExist.

This could be fixed by changing the metaclass that is used here: https://github.com/django/django/blob/1.11.5/django/db/models/base.py#L76 to use a custom metaclass with a __reduce__ method. A patch is in progress for this.

Change History (0)

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