There is a request object of class <class 'django.core.handlers.wsgi.WSGIRequest'> . It looks like this (there may be different parameter values):

  <WSGIRequest: GET '/compare?type=all&type1=transit> 

I get it to a function and pass it to another function in the same place:

 def compareStats(request): ... data1 = get_stats(request) ... data2 = get_stats(request) 

That is, get_stats I need to call twice. And the question is this: I cannot change the get_stats function, but it takes only type as an argument. Is there any way based on the received request to create a new request in which to replace the value of type with the value of type1 ?

Update on the issue in the comments.

What is the problem: the function get_stats gets the values ​​from the database in the form of a json-array. To do this, you need to send a request to the database, in which the values ​​received from the request are substituted. Something like

 sql = """ select data, attr from record where type = {0}""".format(_type) cursor.execute(sql) value, attrs = cursor.fetchall()[0] 

but the query is more complicated. Based on the request returns

 return JsonResponse({'data': response}) 

In two different calls to get_stats , different parameters should be passed (but the request object is originally the same, that’s the problem for me) and you should get different results. Another solution is to slightly change the logic of get_stats , instead of get_stats use another function. This solution has already been applied and is working. But it seems to me that it is more logical and easier to redefine the request object, which is passed as a parameter to the get_stats function, if possible.

And so the question is exactly this - is it possible to make a new request based on the received? Will this be right and good? If not, what is the right thing to do in my case?

  • I apologize for the curiosity. And why is it even necessary? - Mr Fix
  • @Mrfix, data1 should be based on the value of type, and data2 should be based on type1. One data acquisition logic, but 2 different values. Initially, only data1 was needed, but in the course of development I had to use such a crutch. - Mae
  • In principle, it is possible at any time to substitute values, at least in the presentation itself. It is not clear what the complexity. It is not clear why it might be needed. Never encountered such a thing. - Mr Fix
  • @Mrfix if you publish the answer, how to transfer the data2 to the requested data2 function with the body <WSGIRequest: GET '/compare?type=transit> , I will be grateful. And then one does not understand what the difficulty is, while others do not know how to do it. - Mae
  • You would write what the task is. Because there are suspicions that there may be another solution, a more legal one. - Mr. Fix

1 answer 1

def exportStats (request): _type = request.GET.get ('type', 'all')

The request.GET attribute is just a dictionary. You can substitute any values ​​there. In this case:

  request.GET['type'] = "моё значение" def compareStats(request): data1 = get_stats(request) request.GET = {'type': "мои параметры", } data2 = get_stats(request) def compareStats(request): data1 = get_stats(request) # чтобы сделать QueryDict изменяемым надо выполнить его метод copy() request.GET = request.GET.copy() request.GET['type'] = "Мои параметры" data2 = get_stats(request) 

If, of course, there is no way to fix the get_stats interface, then such a crutch is probably the most correct one.

  • request.GET.get('type') - Mr Fix
  • dict.get() is a dictionary method. - Mr Fix
  • According to your solution, the error is: Exception Value: This QueryDict instance is immutable - Mae
  • In fact, it is possible to fix get_stats. Is that right? - Mae
  • I don't know what your code is. Try simply to substitute a QueryDict jung for a pit-dict. Replaced the decision. - Mr. Fix