2016-09-02 8 views
0

イム:DjangoのクエリフィルタパラメータとQ

  variables = Indicator.objects.filter(
       type = constants_type_indicator_variable.variable, 
         Q(current_state = constants_current_state.valid) | 
         Q(current_state = constants_current_state.current_registered) | 
         Q(current_state = constants_current_state.re_opened) | 
         Q(current_state = constants_current_state.open_registered) 
      ) 

しかし、イム最初の「Q」の行で、このエラーを取得:

non-keyword arg after keyword arg 

私はそれが動作する「タイプ」フィールドでフィルタリングすることなく、「Q」を使用している場合は、それがクラッシュtogheter ...

任意のアイデアなぜ?、事前に感謝します。

答えて

1

はこのお試しください:

variables = Indicator.objects.filter(

        Q(current_state = constants_current_state.valid) | 
        Q(current_state = constants_current_state.current_registered) | 
        Q(current_state = constants_current_state.re_opened) | 
        Q(current_state = constants_current_state.open_registered), 
        type = constants_type_indicator_variable.variable, 
     ) 

をそのエラーは、一般的にPythonの機能に非キーワード引数が最初に渡される必要があり、非キーワードの前にkwargsからを通過した後、キーワード引数たことを意味します。これはすべてのPython関数に適用されます。

このpostには、機能的なキーワードの説明があります。

+0

偉大な男、高校生! – jsanchezs

1

@ kt14の答えは正しいが、これはQで区切られたクエリの代わりにinを使用することで簡略化できると思う。有効な州のリストを定義し、それを次のように渡すことができます。

valid_states = [ 
    constants_current_state.valid, 
    constants_current_state.current_registered, 
    constants_current_state.re_opened, 
    constants_current_state.open_registered 
] 

variables = Indicator.objects.filter(
    type=constants_type_indicator_variable.variable, 
    current_state__in=valid_states 
)