Default values for One2Many field

How to add default lines for One2Many field, I know method default_field should work like Many2One does, but when I return product_product.id 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:
@staticmethod
def default_items(cls):
prods = Pool().get(‘product.product’)
items = prods.search((‘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.

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.

1 Like

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

returning a list with dict of values:

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.