I created a method that loops over all pos orders to get data from there to use it on report, I need to display this data on an One2many field Before printing it, I'm trying a lot but the data created on a model and doesn't displayed on current field
here what I did
data = fields.One2many(comodel_name="onepos.report.line", inverse_name="report", string="", required=False, ) def getreport(self): data = self.env['pos.order'].search([]) datas=[] pos_dict= {} report= self.env['onepos.report.line'].search([]) for rec in data: con = rec.config_id.name for line in rec.lines: if con in pos_dict.values(): print('hiiiiiiiiiii') # total_sales = pos_dict[rec.config_id.name] total_sales += line.price_subtotal_incl res = { 'total': total_sales, 'pos': con, } datas.append(res) else: total_sales = line.price_subtotal_incl res2 = { 'total': total_sales, 'pos': con, } datas.append(res2) if datas: print(len(datas), ) print(datas, ) cntr = defaultdict(Counter) for d in datas: cntr[d['pos']].update(d) datas = [dict(v, **{'pos': k}) for k, v in cntr.items()] report.create(datas)
this is related model
class reporttotalline(models.Model): _name = 'onepos.report.line' pos = fields.Char(string="", required=False, ) total = fields.Char(string="", required=False, ) report = fields.Many2one(comodel_name="onepos.report", string="", required=False, )
I am not 100% sure if this is what you are looking for but you might want to try is using the tuple syntax for One2Many / Many2Many fields. (Can be found here)
For example if you were setting One2Many field data:
def getdata(self):
self.write({'data': (0, 0, values_dict)})
This code would create and link a new record to the One2Many field using the values in the values_dict dictionary.
how can assign values_dict or where can i get it
i used
self.write({'data': (0, 0, datas)})
and its return error
if act[0] == 0:
TypeError: 'int' object is not subscriptable