首页 > 解决方案 > 注释“...”与模型上的字段冲突

问题描述

我想写相当于

select coalesce(product_id, -1) as product_id from my_table

在django。然而,尝试

MyTable.objects.values(product_id=Coalesce('product_id', -1))

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

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

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

标签: pythondjango

解决方案


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

MyTable.objects.values(change_product_id=Coalesce('product_id', -1))

推荐阅读