merged and ported to django 1.5