Fix issue 178

Solution inspired in the fix to Django's ticket 20207 https://code.djangoproject.com/ticket/20207 (https://github.com/django/django/commit/216580e03499e035b93283df00fcc03b346fef7b).

A regression test case should be added.