2017-04-08 6 views
0

を通過するとき、私はURLにjQueryの配列を渡し、次のスクリプトを有する:ジャンゴ - INTの無効なリテラル()ベース10と:「」配列

jQuery(document).ready(function($) { 
     $("#continue").click(function() { 
      var selected = $("#meds").bootgrid("getSelectedRows"); 
      var url = "{% url 'meds:prescription' 'test' %}"; 
      url = url.replace('test', selected); 
      window.location = url; 
     }); 
    }); 

及び次のビューを:

class PrescriptionView(generic.ListView): 
    template_name = 'meds/prescription.html' 
    context_object_name = 'meds' 
    model = Medicament 

    def get_queryset(self): 
     return Medicament.objects.filter(id__in=self.kwargs['selected']) 

選択したアレイは[3, 4]ようになりますし、URLがhttp://127.0.0.1:8000/prescription/3,4/

のようなルックスを渡され、私はエラーを取得しておいてください。

ValueError at /prescription/3,4/ 
invalid literal for int() with base 10: ',' 

私はちょうど私がリスト/アレイの項目にIDを比較することができるので、私はフィルタでid__inを用いて渡された配列は、Pythonのリストとして扱われることを想定。

答えて

2

id__in is expecting an array。 渡すものは文字列です。

ids = map(int, self.kwargs['selected'].split(",")) 
return Medicament.objects.filter(id__in=ids) 

は基本的に、あなたが区切り文字で分割し、配列を作成している:ボンネットの下に、int形式

に、配列の個々の要素にアクセスしようとしているあなたは、このような何かを行うことができますids。

デモ:

>>> x = "3,4" 
>>> map(int, x.split(",")) 
[3, 4] 
>>> 
関連する問題