Django无法确定简单的注释函数

2024-09-27 09:36:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在一个queryset类上创建一个注释,它只是添加一个布尔值,这是一些标准查询的结果。在

CustomQueryset(models.QuerySet):
    """ An extension of the traditional queryset to support
        filtering on accepting_offers """

    def annotate_with_accepting_offers(self):
        """ Add a lovely little variable to the SELECT that
            says if the listing is accepting offers.

            A <thing> is accepting offers when its:
                + not cancelled
                + expire date is today or in the future
                + has spaces left
        """
        return self.annotate(accepting_offers=Q(cancelled=False) & Q(expire_date__gte=date.today()) & Q(spaces_left__gt=0))

    def accepting_offers(self):
        """ Annotate with 'accepting_offers' and filter the results that are True """
        return self.annotate_with_accepting_offers().filter(accepting_offers=True)

    def not_accepting_offers(self):
        """ Annotate with 'accepting_offers' and filter the results that are False """
        return self.annotate_with_accepting_offers().filter(accepting_offers=False)

可惜这不管用,有什么想法注释会怎样?在

如果这是SQL,那么最上面一行应该是:

^{pr2}$

编辑: 我打算创建这个注释的原因是为了使对变量的过滤更容易,这可以在接下来的两个函数中看到。在

这两种方法将在更大的查询链中使用,因此(具有讽刺意味的是)通过注释保持简单应该会有所帮助。在


Tags: thetoselffalsedatereturnthatis

热门问题