Further work on PR #2447 for fixing ticket 16735

See https://code.djangoproject.com/ticket/16735