2012-05-23 2 views
7

난 내가 랩 기능에 의해 사용되고있는 콘텐츠 _ 및 OBJECT_ID 내 템플릿과 JS에서 사용하는 코드 변환하려고 :업데이트 request.POST 또는 request.GET

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) 

을 내가 붙어있는 지점은 요청의 갱신이다. POST/request.GET QueryDict. Django는 그 dicts를 불변으로보고합니다. 어떻게 업데이트 할 수 있습니까?

djangodocs에서 .update는 거기에 설명 된 "마지막 값 논리"를 사용할 것이라고 생각했습니다. 그러나 그것은 일어나지 않습니다.

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

여기에 SO에이 주제에 a somewhat similar question가 있지만, 만족스러운 대답을 얻었다 결코 : 복사본을 생성 및 request.GET에 그 재 할당하는 것은 중 하나가 작동하지 않는 것 같습니다. 그 질문에 같은 코드를 사용하여 난 그냥 업데이트 한 후 request.POST 또는 request.GET에 대한 널 수익을 얻을 :

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

ipdb> request.GET 
ipdb> 

그래서 나는 이것에 대해 무엇을 할 수

?

답변

11

update(...) 메서드에는 반환 값이 없으므로 인스턴스가 현재 위치에서 업데이트됩니다. 따라서 request.GET = request.GET.copy().update(ud_dict) 대신에 다음을 작성해야합니다.

request.GET = request.GET.copy() 
request.GET.update(ud_dict) 
관련 문제