我有以下型号:
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
至少有一个Entry
关联的 s(我认为这很古怪):
collections_qs = Collection.objects.annotate(
entry_count=Count('entries')
).filter(entry_count__gt=0)
效果很好。但我想将此查询与两个datetime
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
的 s 的顺序而不是内容。如何实现日期/时间搜索(最好只使用一个查询)?