class ImpersonateMiddleware(object):

    def process_request(self, request):
        if request.user.is_superuser and '__impersonate' in request.GET:
            request.user = models.User.objects.get(id=int(request.GET[
                '__impersonate']))

def process_response(self, request, response):
    if request.user.is_superuser and '__impersonate' in request.GET:
        if isinstance(response, http.HttpResponseRedirect):
            pass
Django user impersonation by admin