首页 > 解决方案 > Annotate 中的 ExpressionWrapper 在 django 中不起作用

问题描述

from django.db.models import F, ExpressionWrapper

resource_list = Resources.objects.filter(active=True, status__id=STATUS_PUBLIC_ID)

resource_list = resource_list.annotate(rank=ExpressionWrapper(
                            similar(keywords, F('title')), output_field=DecimalField()
            )).filter(rank__gt=0).order_by('-rank').distinct()

def similar(a, b):
print(a, b)
return SequenceMatcher(None, a, b).ratio()

当我运行上面的代码时,它给了我一个错误“TypeError:'F' object is not iterable” Resources 是我拥有的模型,它有一个字段标题,similar 是一个检查 2 个字符串之间相似性比率的函数

标签: djangopython-3.x

解决方案


推荐阅读