I try to translate a code i use in my templates and js to a content_type and object_id that is being 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's 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 where i am stuck is the updating of the request.POST / request.GET QueryDict. Django reports those dicts as being immutable. How can i update them?
From the djangodocs i thought .update would use the "last-value logic" described there, which i would be perfectly fine with. But that's not happening. Creating a copy and reassigning that to request.GET doesn't seem to work either:
request.GET = request.GET.copy().update(ud_dict)
There is a somewhat similar question on this topic here on SO, but it never got a satisfying answer. Using the same code as in that question i just get a null return for request.POST or request.GET after updating:
request._get = request.GET.copy()
import ipdb;ipdb.set_trace()
ipdb> request.GET
ipdb>
So what can i do about this?
The update(...)
method does not have a 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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With