注释“...”与模型上的字段冲突

blu*_*ote 5 python django

我想写 django 相当于

select coalesce(product_id, -1) as product_id from my_table
Run Code Online (Sandbox Code Playgroud)

在 Django 中。然而,尝试

MyTable.objects.values(product_id=Coalesce('product_id', -1))
Run Code Online (Sandbox Code Playgroud)

给我错误:The annotation 'product_id' conflicts with a field on the model.

这迫使我使用不同的名称,并在 python 中重命名生成的字典,这比在数据库中慢得多。

有没有办法告诉 django“我知道我在做什么,继续”???

Sar*_*Rai 3

尝试不同的名称而不是您的模型属性,

MyTable.objects.values(change_product_id=Coalesce('product_id', -1))
Run Code Online (Sandbox Code Playgroud)