Fix ticket #10164

Import patch, add test and documentation.

https://code.djangoproject.com/ticket/10164