Django Query:在TimeField中使用`seconds`差异编译数据

问题描述 投票:1回答:1

我的Django应用程序中有以下模型:

class PeopleCount(models.Model):
    """
    A webapp model classs to store People Count Details.
    """
    timestamp = models.DateTimeField(auto_now=True)
    people_count_entry = models.IntegerField(blank=True, null=True)
    people_count_exit = models.IntegerField(blank=True, null=True)
    store = models.ForeignKey(Store, blank=True, null=True)
    profile = models.ForeignKey(Profile)
    camera = models.ForeignKey(Camera)
    recorded_time = models.DateTimeField(null=True, blank=True)

    def __str__(self):
        return "People Count {}".format(self.timestamp)

    class Meta:
        verbose_name = "People Count"
        verbose_name_plural = "People Count"
        ordering = ['-recorded_time']

我想打印出PeopleCount的值,如果两个对象的登记时间之间的差异小于1秒,我可以聚合它们的值。例如:

  • 假设我在11:30:21得到PeopleCountObjA - >(people_count_entry为4)
  • 然后我得到另一个值PeopleCountObjB - > 11:30:22(其中people_Count_entry为2)
  • 我应该得到答案,因为Group Count应该是6 时间11:30:21和11:30:22是指模型中的recorded_time字段。

我尝试使用聚合查询,但无法输入过滤器的差值为1秒。

希望我对这个问题很清楚。 TIA

django django-queryset django-aggregation
1个回答
0
投票

AFAIK,您无法执行此操作,因为Django不提供对查询集的组合检查/注释。 从逻辑上讲,你试图比较彼此的对象,可能有nC2组合(如果你有100个对象,你需要检查4950组合,100C2=4950),这是无法通过数据库/ django orm完成的

© www.soinside.com 2019 - 2024. All rights reserved.