2012-05-23 2 views
7

を使用して、私はラップ機能で使用されているCONTENT_TYPEとの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はこれらのディクテーションを不変であると報告します。どうすれば更新できますか?

djangodocsから私が思うに、「最後の値の論理」がそこに書かれていると思います。しかし、それは起こっていない。

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

ここSO上でこのトピックに関するa somewhat similar questionありますが、それは満足のいく答えを得たことはありません:コピーを作成し、request.GETにそれを再割り当てすることはどちらか動作するようには思えません。私はちょうど更新した後、request.POSTまたはrequest.GETの場合はnull戻り値を取得し、その質問と同じコードを使用する:

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) 
関連する問題