问题
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" 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
with :
self.x_signatures = ids[0]
or :
self.x_signatures = ids[1]
It displays the record, so what's going on ?
回答1:
Odoo has a very specific set of rules about how you are "allowed" to manipulate One2many and Many2Many fields.
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)]
来源:https://stackoverflow.com/questions/54245863/odoo-tree-view-only-show-one-record-with-compute