我需要在模型字段上使用自定义函数 agr_suitable_code
注释查询集。
看起来像这样
def strip_accents(s):
return ''.join(c for c in unicodedata.normalize('NFD', s)
if unicodedata.category(c) != 'Mn')
def agr_suitable_code(on_distro_code):
upper_str = on_distro_code.upper()
return strip_accents(upper_str)
这里没有什么特别的,该函数接受一个字符串并在进行一些更改后返回它。
然后我尝试用它进行注释,因为没有这个 agr_like_code
注释字段就无法实现以下查询。
related_params[distro.id] = table.objects.filter(
ProductId__in=[map["ProductId"] for map in related_maps_by_distro.values("ProductId")]
).annotate(
agr_like_code=Func(F("DistributionParameterId__Code"), function=agr_suitable_code),
prod_to_param_exists=
Exists(AgrDistributionProductParameter.objects.filter(ProductId=context['product'],
DistributionParameterId__Code=OuterRef(
"agr_like_code")).values('id')),
has_agr_param=Case(
When(Q(agr_like_code__in=agr_parameter_codes)
& Q(DistributionParameterId__Code__in=agr_param_map_codes_distro)
, then=2),
When(Q(agr_like_code__in=agr_parameter_codes), then=1),
default=0,
output_field=models.IntegerField(),
etc ..
我试图制定这一行agr_like_code=Func(F("DistributionParameterId__Code"), function=agr_suitable_code)
在很多方面都没有成功。
我收到的当前错误是
django.db.utils.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect syntax near '<'. (102) (SQLExecDirectW); [42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect syntax near '<'. (102); [42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect syntax near '<'. (102); [42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect syntax near '<'. (102); [42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Statement(s) could not be prepared. (8180)")
我知道 Django 文档建议编写自定义 SQL 查询,例如
class ConcatPair(Func):
...
function = 'CONCAT'
...
def as_mysql(self, compiler, connection, **extra_context):
return super().as_sql(
compiler, connection,
function='CONCAT_WS',
template="%(function)s('', %(expressions)s)",
**extra_context
)
但我不能用 agr_suitable_code
函数真正做到这一点。
有没有办法用这个自定义的简单函数来注释查询集?任何提示将不胜感激。提前致谢。