0
votes

I created a many2one field to select a product in a form. I filter the possible items to choose with a domain:

light_system = fields.Many2one(
    'product.product',
    string='Lighting System',
    ondelete='restrict',
    domain=[('categ_id.name', '=','Lighting System')]
)

If I create a new item directly from this dropdown with "create new" the category is not set to 'lighting system' therefore it won't show up when I try to use it again, risking duplicates of the same item by different users... Is it possible to get the category to be set to "Lighting System" by default when I choose to create a new item directly from this field with 'create new""' or 'create and edit""' ?

I tried

<field name="light_system"
       placeholder="Lighting System"
       context="{'default_categ_id':'Lighting System'}"/>

and

<field name="light_system"
       placeholder="Lighting System"
       context="{'default_categ_id.name':'Lighting System'}"/>

in the form view, but neither works

1

1 Answers

0
votes

You need to pass the category id:

<field name="light_system"
   placeholder="Lighting System"
   context="{'default_categ_id':lighting_id}"/>

if you have it as field into the form, even hidden

If you created the category in a data.xml of some module, you can use ref:

<field name="light_system"
   placeholder="Lighting System"
   context="{'default_categ_id':ref('name_of_the_module.lighting_id')}"/>