Class A has a One2many
field Wproductlist
which points to Class B. i want to update the quantity field of stock.quant class by subtracting quantity_needed field of classB. how can i achieve this? I tried the below code, but it shows singleton error when the class B has multiple records.
Class A:
wproductlist = fields.One2many(comodel_name="class a",
inverse_name="ppp", string="Product List")
@api.multi
def update:
d = self.env['stock.quant'].search([('product_id', '=',
self.wproductlist.productname)])
for record in d:
record.write({'quantity':quantity})
Class B:
_name = "classa"
productname = fields.Char(string="Product Name")
ppp = fields. Char(string = "dummy")
quantity = fields.Char(string="Product Available")
I can see the where the error is even with the syntax errors as well as the errors in the field definition:
so the error is when you call models.Model.search()
method you write in your domain [('product_id', '=', self.wproductlist.productname)]
and in the definition of the model you defined wproductlist
to be One2many field, meaning that self.wproductlist
might (and in most of the cases) refer to more than one record by definition.
so when you try to get the productname
from self.wproductlist
which is referring to multiple records?
if you want to get all the values of productname
in all records in self.wproductlist
you should use the .mapped()
method:
product_names = self.wproductlist.mapped('productname')
in this case the product_names
is a list of all values of productname
field in all records referred to by self.wproductlist
in this call you have to change the operator to be in
instead of =
d = self.env['stock.quant'].search([('product_id', 'in',
self.wproductlist.mapped('productname'))])
this should solves your issue.
regards.