私は次のモデルを持っています:
class Collection(models.Model):
# some fields here ...
class Entry(models.Model):
collection = models.ForeignKey(Collection, reverse_name="entries")
start_time = models.DateTimeField()
end_time = models.DateTimeField()
Collection
ここで、少なくとも1つが関連付けられているすべてのをクエリしたいと思いますEntry
(かなり風変わりだと思います)。
collections_qs = Collection.objects.annotate(
entry_count=Count('entries')
).filter(entry_count__gt=0)
これは非常にうまく機能します。datetime
しかし、このクエリを2つのsstart
とで指定された日付/時刻ウィンドウ検索と組み合わせたいと思いますend
。私が思いついたのは:
if start:
collections_qs = collections_qs.filter(entries__start_time__gte=start)
if end:
collections_qs = collections_qs.filter(entriess__end_time__lte=end)
ただし、これは返されたCollection
の順序を並べ替えるだけで、コンテンツは並べ替えません。日付/時刻検索を実行するにはどうすればよいですか(できれば1つのクエリだけで)?