跳至内容
菜单
此问题已终结
2 回复
727 查看

Dear All,


I am trying to add a field in purchases to display all the Partners from the Sale Orders that are linked to the Purchase.


I have the following code, and I am facing an issue:


When I do not use “store=True“ the function works correctly and displays the partners. However, in order to save the Partners in Purchases that are not linked to any sale order (since I have a compute method in my field), I set “store=True“ but then the first case does not work.


In other words, when I do not use store, the function works correctly and the customer is displayed. However, when I set store, the function does not work, and the related partners do not appear, although the new partners are saved correctly in the Purchase.


What am I missing, and how can I fix this?

class PurchaseOrder(models.Model):
_inherit = 'purchase.order'

name_ids = fields.Many2many(comodel_name='res.partner',compute='_compute_customer_names',
string="Customers",store=True)

def _compute_customer_names(self):
for purchase in self:
sale_order_ids = purchase._get_sale_orders()
for sale_order in sale_order_ids:
purchase.name_ids = sale_order.partner_id
形象
丢弃
最佳答案

Hello,

if you want to store the field in DB with store =True result you must to to specify the dependencies on the compute method with the decorator depends()

Thanks.

形象
丢弃
编写者

I had tried using 'depends()' as well, but it still didn't work.

The value of a computed field usually depends on the values of other fields so you need to specify these fields in @api.depends() so if any of these fields in changed then the computed field will be recomputed again.

If you added the property store=True to the field which already created, It will not be recomputed. Try to test it with creating a new purchase order after adding @api.depends()

最佳答案

Hi,


If you want to store a computed field in the database, the field must depend on one or more other fields (via @api.depends).

Try the following code


class PurchaseOrder(models.Model):
_inherit = 'purchase.order'

name_ids = fields.Many2many(comodel_name='res.partner',compute='_compute_customer_names',
string="Customers",store=True)

@api.depends('order_line.sale_order_id')
def _compute_customer_names(self):
for purchase in self:
sale_order_ids = purchase._get_sale_orders()
if sale_order_ids:
for sale_order in sale_order_ids:
purchase.name_ids = sale_order.partner_id
else:
purchase.name_ids = None

Hope it helps

形象
丢弃
相关帖文 回复 查看 活动
0
8月 19
5337
compute issue 已解决
1
5月 21
3591
2
6月 20
6299
1
11月 24
1004
2
8月 22
5100