Skip to Content
Menu
This question has been flagged
3 Replies
15392 Views

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, )


Avatar
Discard

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.

Author

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

Best Answer

hi,

to write to one2many field use :

(0, 0,  { values })    link to a new record that needs to be created with the given values dictionary
(1, ID, { values })    update the linked record with id = ID (write *values* on it)
(2, ID)                remove and delete the linked record with id = ID (calls unlink on ID, that will delete the object completely, and the link to it as well)
(3, ID)                cut the link to the linked record with id = ID (delete the relationship between the two objects but does not delete the target object itself)
(4, ID)                link to existing record with id = ID (adds a relationship)
(5)                    unlink all (like using (3,ID) for all linked records)
(6, 0, [IDs])          replace the list of linked IDs (like using (5) then (4,ID) for each ID in the list of IDs)
Avatar
Discard
Author

i know that , but how to use it in ,my code

i used

self.write({'data': (0, 0, datas)})

and its return error

if act[0] == 0:

TypeError: 'int' object is not subscriptable

Related Posts Replies Views Activity
0
Nov 22
2535
0
Apr 19
2858
6
Aug 19
60729
1
Mar 21
1341
2
Apr 20
3067