Django - 在查询中使用注释 - 相关字段查找无效

问题描述

在我的经理中,我覆盖了 get_queryset 以注释 sqft_annotated 值。

class RealestateManager(models.Manager):
    def get_queryset(self):
        return super().get_queryset().annotate(
            sqft_annotated=Coalesce('sqft_a','sqft_b','sqft_c')

我经常使用这个值,但现在,在这查询中,它引发了错误(可能是因为它没有被注释):

_a = Q(realestates__home_type__in=[constants.HOME_TYPES.HOME_TYPE__SINGLE_FAMILY,constants.HOME_TYPES.HOME_TYPE__FARM],realestates__sqft_annotated__gte=1100,...)
_b = Q(<QUERY>)
_c = Q(<QUERY>)

leads_query = Q(Q(_a | _b) | ~_c)


Category.objects.annotate(lead_count=Count('realestates',filter=leads_query)).annotate(
            lead_perc=Case(When(lead_count=0,then=0),default=(Count(
                'realestates',filter=leads_query) / float(total)) * 100))

错误

Related Field got invalid lookup: sqft_annotated

我可以在这样的查询中以某种方式使用 sqft_annotated 吗?

PS:试图在 base_manager_name 中设置 class Meta - 没有帮助。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)