0
votes

I have a one2many field in Odoo and I need to populate lines with following method:

def add_lines(self, cr, uid, ids, context=None):
        val = {'value': {'kh_lines_a1': [], 'kh_lines_a2': []}}
        account_move_obj = self.pool.get('account.move')
        line_ids = account_move_obj.search(cr, uid, [('vat_subject', '=', True)], context=context)
        for line in account_move_obj.browse(cr, uid, line_ids, context=context):
            res = {
                'name': line.name,
                "date": "2015-01-01"
            }
            val['value']['kh_lines_a1'].append(res)
        return val

The result should be that new 'kh_lines_a1' are created according to the result from line search, but no data is returned.

As variables are returned correctly I guess there is something I missed with the "return" stuff.

Tried to change the dictionary paradigm but no success.

1
UPDATE: it works in on_change method... - Marek Jan-Alexander
Hi Marek, is it a function that runs behind or an on_change?, the return values are from on_changes only, what you have to do is a write of the object. You can create the lines with the pool and set his foreing key with the id of the main document. self.pool.get('kh.lines.model').create({'name': line.name, 'date': "2015-01-01", parent_id: ids[0]}) or wathever the id is. Try something like that and tell me if it works - dccdany
Hi, thanks for a tip. I tried to bind this function under a button which didn´t work, but it does if I assign it under on_change. The create method seems to work, however the it may happen that the search fc would return hundreds of lines which I need to create hundreds of one2many´s according to. Is it possible to use list of values in create? Thanks for help! - Marek Jan-Alexander
Just use the same loop that you already have, why a lot of one2manys? if you assign the same parent_id its just 1 one2many even if you do a lot of creates, since it relates to the same parent - dccdany

1 Answers

0
votes

try this:

val['value']['kh_lines_a1'].append((0, 0, res))