【发布时间】:2023-03-07 23:07:02
【问题描述】:
我有一个表单,我在其中添加了 CUSTOM FIELD 复选框。数据就是这样加载的
widget = forms.CheckboxSelectMultiple,
queryset = Product.objects.all (),
required = False,
)
当我创建组件模型的新实例时,这是一个很好的解决方案。但是,在编辑的情况下,这是一个问题,因为某些复选框可以填写。所以我需要稍微改变一下这个字段的查询集。我想这样做
prod = Product.objects.annotate (dupa = FilteredRelation ('product', condition = Q (product__component = 120))). Values ('short_name', 'name', 'dupa__component'),
但它得到错误 django.db.utils.ProgrammingError: missing FROM-clause entry for table" t7 第 1 行:...一个 ON ("epm_product"."Id" = ass."Product_id" AND (T7."Comp ...".
查询返回:
选择“epm_product”。“Short_name”,“epm_product”。 “名称”、“epm_productitem_component”。 "component_id" FROM "epm_product" 左外连接 "epm_productitem" dupa ON ("epm_product". "id" = dupa. "product_id" AND (T5. "component_id" = 120)) 左外连接 "epm_productitem_component" ON (ass. "Id" = "epm_productitem_component". "Productitem_id")
为了测试数据及其呈现方式,我使用了通常的 SQL 查询:
"select pr.name, pr.short_name
, (select count (*) from epm_productitem pi left join epm_productitem_component pic on pic.productitem_id = pi.id
where pic.component_id = 120 and pi.product_id = pr.id) ccheck
from epm_product pr
这给了我这个结果
| name | short name | check |
|---|---|---|
| "Pilot small" | "PM" | 1 |
| "Receiver very small" | "BM" | 0 |
| "Receiver large" | "D" | 0 |
| "Medium receiver extended" | "SR" | 0 |
| "Medium receiver" | "S" | 0 |
| "Receiver small" | "M" | 0 |
| "Slave heat pump" | "PC slave" | 0 |
| "Heat pump controller" | "PC LCD" | 1 |
能否请您帮我正确地将自定义字段添加到表单中,以便我可以获得原始 SQL 输出?
【问题讨论】:
标签: django django-models django-forms