2017-10-26 1 views
1

場所に緯度と経度のフィールドを使用するモデルがあります。クエリパラメータを使用して実行するクエリの1つは、特定の半径を中心とした検索です。私が読んで、私はそれをオーバーライドするクエリセットがあります。/admin/dal/listing?radius=50と私の管理ページを呼び出すとき私の独自のクエリパラメータをdjangoの管理者に追加します

queryset = super().get_queryset(request) 
if 'radius' in request.GET: 
    queryset = queryset.in_distance(request.GET['radius'], fields=['location__latitude',location__longitude'], points=[lat, lon]) 
return queryset 

Iは、クエリ文字列なしで管理者にリダイレクトされます。

Djangoのコードを踏襲し、この発見:クエリ文字列が知られているフィールドDjangoが優雅にパニックと管理ページをフィルタリングしていないリダイレクトされていないため、一言で言えば

# At this point, all the parameters used by the various ListFilters 
# have been removed from lookup_params, which now only contains other 
# parameters passed via the query string. We now loop through the 
# remaining parameters both to ensure that all the parameters are valid 
# fields and to determine if at least one of them needs distinct(). If 
# the lookup parameters aren't real fields, then bail out. 
try: 
    for key, value in lookup_params.items(): 
     lookup_params[key] = prepare_lookup_value(key, value) 
     use_distinct = use_distinct or lookup_needs_distinct(self.lookup_opts, key) 
    return filter_specs, bool(filter_specs), lookup_params, use_distinct 
except FieldDoesNotExist as e: 
    six.reraise(IncorrectLookupParameters, IncorrectLookupParameters(e), sys.exc_info()[2]) 

を。

どうすればよいですか?

答えて

0

あなたはリクエストから、カスタマーのパラメータをポップする必要があります。これを試してください:

def get_queryset(self, request): 
    queryset = super().get_queryset(request) 
    request.GET = request.GET.copy() 
    radius = request.GET.pop('radius', None) 
    if radius: 
     queryset = queryset.in_distance(
      radius[0], fields=['location__latitude', 'location__longitude'], 
      points=[lat, lon]) 
    return queryset 
+0

ありがとう、私は書いたことをよく見て、djangoはモデルの一部ではないパラメータを削除します。 'radius'はモデルの一部ではありません –

+0

私は今同じ問題を解決しました。私はモデルの一部ではないクエリパラメータを渡そうとしましたが(** number **)、_/applabel/users /?e = 1_にリダイレクトしました。上記のコードを追加しました。 。 _/applabel/users /?number = 5_というページのURLを更新して、正しい結果(**番号**でフィルタリング)を取得します。 (Django1.11) –

+0

私はポップの部分を逃した、良いキャッチ、チェック... –

関連する問題