Update .POST request or request.GET using decorator

I am trying to translate the code that I use in my templates and js into content_type and object_id, which are used by the wrapped function:

def translate_modelcode(function=None,redirect_field_name=None): """ translate an item-code specified in settings to a content_type and the item-id to the object_id """ def _decorator(function): def _wrapped_view(request, *args, **kwargs): item_code=request.REQUEST.get('item-code',None) if item_code: object_id = request.REQUEST.get('item-id',None) # resolve_modelcode get the models name from settings content_type = resolve_modelcode(item_code) ud_dict = {'content_type':content_type, 'object_id':object_id} if request.method == 'GET': request.GET.update(ud_dict) else: request.POST.update(ud_dict) return function(request, *args, **kwargs) return _wrapped_view if function is None: return _decorator else: return _decorator(function) 

The point I'm stuck at is updating the .POST / request.GET QueryDict request. Django reports that these dictations are unchanging. How can I update them?

From djangodocs, I thought that .update would use the "last value" logic described there, which I would do just fine. But this does not happen. Making a copy and reassigning this .GET request does not work either:

 request.GET = request.GET.copy().update(ud_dict) 

There is a somewhat similar question on this topic here, but it has not received a satisfactory answer. Using the same code as in this question, I just get zero return for the request. POST or request.GET after update:

 request._get = request.GET.copy() import ipdb;ipdb.set_trace() ipdb> request.GET ipdb> 

So what can I do with this?

+7
source share
1 answer

The update(...) method has no return value; it updates its instance in place. So instead of request.GET = request.GET.copy().update(ud_dict) you should write

 request.GET = request.GET.copy() request.GET.update(ud_dict) 
+11
source

All Articles