代码之家  ›  专栏  ›  技术社区  ›  Milano

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

  •  0
  • Milano  · 技术社区  · 3 年前

    在我的经理身上,我越权了 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
    

    我能用一下吗 平方英尺_未注明 在这样的询问中?

    PS:尝试设置 base_manager_name 在里面 class Meta -没有帮助。

    0 回复  |  直到 3 年前