Reputation: 526
i'am added a method in product.template to compute the product name form strings of some custom fields that related to other models , i added store=True to the name field so i can search about it , but when related fields change on their models the filed name dosn't change , so i need to add server action to menuitem called "update product name" which be located on cutome app that the related fields are there ,if i pressed on this menuitem it run the metode of naming and get new updated field on product name how can i do that here is the code
class autopart(models.Model):
_inherit = 'product.template'
@api.multi
@api.depends('item','dsc', 'drc', 'org','car','model', 'manf','year')
def naming(self):
for rec in self:
if rec.year:
rec.name = " ".join(
[rec.item and rec.item.name or "", rec.drc and rec.drc.name or "", rec.dsc and rec.dsc.name or "",
rec.org and rec.org.name or "", rec.manf and rec.manf.name or "", rec.car and rec.car.name or "",
rec.model and rec.model.name or "",rec.year and rec.year.name or ""
])
else:
rec.name = " ".join(
[rec.item and rec.item.name or "", rec.drc and rec.drc.name or "", rec.dsc and rec.dsc.name or "",
rec.org and rec.org.name or "", rec.manf and rec.manf.name or "", rec.car and rec.car.name or "",
rec.model and rec.model.name or "",
])
name = fields.Char(string="Name", compute=naming ,store=True , required=False,)
here is menuitem
<menuitem id="update_products_menu" name="Update products" parent="master.menu_category_main" sequence="1" action="action_update_products"/>
server action
<record id="action_update_products" model="ir.actions.server">
<field name="name">action_update_products</field>
<field name="type">ir.actions.server</field>
<field name="model_id" ref="model_updateproducts"/>
<field name="state">code</field>
<field name="code">how can i run naming methode from product.template here</field>
</record>
Upvotes: 1
Views: 158
Reputation: 14721
Your compute fields depend on the name of the other items, in odoo you can depend on related fields like this and you can go as deep as you want, and not only many2one you can use X2many too
@api.depends('item.name','dsc.name', 'drc.name', 'org.name','car.name','model.name', 'manf.name','year.name')
This way when ever you change one of this names even in some other view the field will be recomputed in background. I hope this helps you no need to create button for this task
Upvotes: 2