Backport 'DoesNotExist is not picklable' fix

https://code.djangoproject.com/ticket/17776

https://github.com/django/django/commit/a54a8bab0c6a96c03452040e92b2a3141695a363