2

次のクエリを実行するとします。

showtimes = ShowTime.objects.filter(
    start_date__lte=start,
    end_date__gte=end,
    movie__slug=movie.slug,
    city=city,
    visible=1)

queryset object次に、それを取り込んで、次のような他の属性に基づいて結果をさらにフィルタリングする関数が必要です。

def is_subtitled_3d(showtimes):
    return (
        showtimes.language == LANGUAGE_SUBTITLED and
        showtimes.type_vip == None and
        showtimes.type_3d == 1 and
        showtimes.type_gtmax == None and
        showtimes.type_xd == None)

そのようなものはオブジェクトを変更するために機能しますか、それともそれを行う別の方法がありますか?

4

1 に答える 1

3

クエリセットは怠惰で連鎖可能です

showtimes何度でもフィルタリングできます。あなたの構文が正しいとは思いませんが、標準を使用しfilterてクエリセットをフィルター処理し続けることができます

def is_subtitled_3d(showtimes):
  return showtimes.filter(
    language=LANGUAGE_SUBTITLED,        
    type_vip__isnull=True,
    type_3d=1,
    type_gtmax__isnull=True,
    type_xd__isnull=True
  )

ユーザーが 3D ムービーをフィルター処理したい場合、フィルターを組み合わせる方法を説明するために、次のようにします。

showtimes = ShowTime.objects.filter(
    start_date__lte=start,
    end_date__gte=end,
    movie__slug=movie.slug,
    city=city,
    visible=1)

if request.GET.get('is_3d_movie'):
  showtimes = showtimes.filter(type_3d=1)
etc...
于 2013-02-20T20:42:51.660 に答える