Default values for One2Many field

(German Dario Alvarez) #1

How to add default lines for One2Many field, I know method default_field should work like Many2One does, but when I return didn’t work for me.

In my use case most of sale products are always same 2-5 I wanted to have default these on One2Many by default, so user can add new or delete existences.

What I think is possible:
def default_items(cls):
prods = Pool().get(‘product.product’)
items =‘code’, ‘=’, ‘item1’))
# use de cls.items_one2many_field (‘add’, items) here?
# how properly use that add call to link products by deafult?
return items[0].id

Sorry if the question is not advance level, these are new things to me.

(Cédric Krier) #3

You can not use ids as default value for One2Many because by nature the content of One2Many is new records. So you must return a list of dictionaries which contains the default value for each new line.

But I think you should reconsider using default value for such case because it can have side effect when creating parent record from different place. You can use a instance button or restrict the default value for a specific action window using a context.

(German Dario Alvarez) #4

Thanks @ced, I’ll take a look for button, until now this solution like you said worked for me:
def default_my_one2many_field_name():
return [{“field1”:“value1_”,“field2”:“value2”}]

returning a list with dict of values: