1
votes

I want to update computed field records but it writes self record in all fields when I use write method.

the name field in product, template is non stored. now because of our business needs so I can't search it.

I make another stored field to move name records into it. so I can looking for instead of name.

I make a button to update data in that field from name field. but when I press the button it updates with self name not name of each product.

class autopart(models.Model):
_inherit = 'product.template'

@api.multi
def button_name(self):
    for rec in self:
        name=self.name
        rec.search([]).write({'nameseacrh': self.name})



name = fields.Char(string="Name", required=False ,compute=compute_amount ,search=pro_search)

nameseacrh = fields.Char(string="", required=False,store=True, compute=compute_search )

button XML

<button name="button_name" type="object" string="name" class="oe_highlight" groups="base.group_system"/>
1

1 Answers

0
votes

I don't fully understand what you are trying to do but here are already a few remarks:

@api.multi
def button_name(self):
    for rec in self:
        name=self.name  # remark 1
        rec.search([]).write({'nameseacrh': self.name})  # remark 2 and 3

1) You never use this variable

2) rec.search([]) is the same as self.env['product.template'].search([]): It takes all the product templates in the db. You probably didn't want to do that... Then you write your values on all the records.

3) you use self.name instead of rec.name. The first one will work only if self is a recordset of one element, which will not be the case most of the time in an @api.multi

4) I don't get why you would need to make a new field that does what you had before. You shouldn't have to do that.