私は、Djangoの1.11にと私は私が読んだもの結合したいと思います。たとえばジャンゴ:get_querysetで条件式(自己)
- https://docs.djangoproject.com/fr/1.11/ref/models/conditional-expressions/
- https://docs.djangoproject.com/fr/1.11/topics/class-based-views/generic-display/
- https://docs.djangoproject.com/fr/1.11/ref/models/querysets/#django.db.models.Q
オブジェクトがユーザー領域にあり、ListViewがそれを使用しているかどうかをチェックするようなものがあるとします。私はDEF(自己)get_queryset にフィルタの内側に、この条件チェックを移動したい何
open_help_requests = HelpRequest.objects.filter(state=HelpRequest.OPEN)
filtered_help_requests = []
for help_request in open_help_requests:
""" Formule de Haversine pour connaitre la distance entre deux points géographiques """
earth_radius = 6373.0
lat1 = radians(help_request.student.studentcollaborator.postal_code.latitude)
lon1 = radians(help_request.student.studentcollaborator.postal_code.longitude)
lat2 = radians(request.user.student.studentcollaborator.postal_code.latitude)
lon2 = radians(request.user.student.studentcollaborator.postal_code.longitude)
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat/2) ** 2 + cos(lat1) * cos(lat2) * sin(dlon/2) ** 2
c = 2 * atan2(sqrt(a), sqrt(1 - a))
distance = earth_radius * c
if distance <= help_request.settings.distance:
filtered_help_requests.append(help_request)
:私は直接フィルタクエリセットと追加のシンプルなオーダー/フィルター操作を行うことができるように。フィルタリングされた変数リストのIDが重すぎるように見えるクエリセットを再作成します(このように:Django get a QuerySet from array of id's in specific order)。
アイデア?
ありがとうございました。
ありがとう:問題は、私はちょうどその結果に基づいてフィルタリング(パートを追加)し、別のフィルタをしたいです。各ステップでクエリーセットを再作成することは重い – jy95