Odoo树视图仅显示一条包含计算的记录 [英] Odoo tree view only show one record with compute
问题描述
我试图在用户页面上显示用户签名的文档(来自"Sign app"),所以我将其添加到继承的模型中:
I'm trying to display a user signed documents (from the "Sign app") on his page, so I added this to the inherited model:
x_signatures_relation = fields.One2many("signature.request.item", "partner_id")
x_signatures = fields.One2many("signature.request", compute="_get_signed_documents")
@api.one
def _get_signed_documents(self):
ids = []
for signature in self.x_signatures_relation:
ids.append(signature.signature_request_id)
self.x_signatures = ids
"signature.request.item"是将伙伴(用户)与实际签名"signature.request"相关的表. 但是,即使当前用户有两个签名,这也会返回一个空视图,但是如果我替换了:
"signature.request.item" is the table relating the partner (user) with "signature.request" the actual signature. However this return an empty view even though the current user has two signatures, but if I replace :
self.x_signatures = ids
与:
self.x_signatures = ids[0]
或:
self.x_signatures = ids[1]
它显示记录,这是怎么回事?
It displays the record, so what's going on ?
推荐答案
Odoo有一组非常具体的规则,涉及如何允许"操作One2many
和Many2Many
字段.
Odoo has a very specific set of rules about how you are "allowed" to manipulate One2many
and Many2Many
fields.
请参见我最近的回答,其中详细说明了所有选项以及何时/如何使用它们. Odoo 文档也对此进行了解释也是
See my recent answer, which gives a detailed explanation of all options and when/how to use them. The Odoo documentation also explains it as well.
在您的情况下,您是在计算方法中设置值,因此您想完全替换所有现有值.
In your case, you are setting the value in a compute method, so you want to completely replace any existing values.
# Instead of
# self.x_signatures = ids
# Try this, which uses the special number 6 to mean
# "replace any existing ids with these ids"
self.x_signatures = [(6, 0, ids)]
此外,您可以简化计算方法:
Furthermore, you could simplify your compute method:
@api.one
def _get_signed_documents(self):
self.x_signatures = [(6, 0, self.x_signatures_relation.ids)]
这篇关于Odoo树视图仅显示一条包含计算的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!