How to change url params (query_string) in django rest framework before handle request?

Because I need compatible with old client. For example, when I recevie an request, i want change the params so

Others: change to,2 change to

Is there any place I can write code before handle the request? I try to change request.environ['QUERY_STRING'] and request.META['QUERY_STRING'] in middleware, but not work.


You can use django's redirect function for this. For that in your old views just redirect it to your new views.

A simple Example -

def my_old_view(request,pk):
    return redirect(f'/api/excersise/?id={pk}')

So if someone enters your old URL it will be redirected to new one without any issues.

I believe you can make use of the RedirectView and override the get_redirect_url method to map the change in query params.

For implementation details, you can refer to the official docs.

Good luck!

Back to Top