This question has been flagged

Hello !

I get troubles to fill fields of my model opened in edit form view.

Here are some screens of the usecase :

http://imgur.com/G1ptWXN

http://imgur.com/lWrrDRj

http://imgur.com/1FvI25l

In the URL, I can see the active_id (or active_ids) of my products on the second screenshot. But my trouble is in filling the field Article with them (WITHOUT saving it : I want the user to choose to save or not)

Here is the code of my "new" button action right now :


def new_match(self):
    return{
        'name':("Save a new matching"),
        'view_mode': 'form',
        'view_type': 'form',
        'view_id': False,
        'res_model': 'label.label',
        'res_id': self.id,
        'type': 'ir.actions.act_window',
        'target': 'current',
        'domain': '[]',
        'flags': {'initial_mode': 'edit',} ,
        '_default':{'products':[(6,0,[self._context.get('active_ids')])]},
  }

I tried a lot of stuff that didn't work, without giving me any error or stuff. I'm a bit lost here

If someone could give me solution or part of it that'd be great ! :)

Thanks !

Avatar
Discard
Author

The code of my list view :

<record model="ir.ui.view" id="label.tree_new_match">

<field name="name">Create Matching</field>

<field name="model">label.label</field>

<field name="view_type">form</field>

<field name="view_mode">tree</field>

<field name="arch" type="xml">

<tree colors="red:status=='OFFLINE';black:len(products)!=0;green:len(products)==0" default_order='len_products, real_id' create='false' delete='false'>

<button string="New" type="object" name="new_match"/>

<field name="description" string="Name"/>

<field name="real_id"/>

<field name="type"/>

<field name="products"/>

<field name="status"/>

<field name="len_products" invisible="context.get('len_products')!=1000"/>

</tree>

</field>

</record>

Author Best Answer

The only way i found to do this was to set the fields direclty in the action function, but that doesn't do what i wanted to do at first :'(

Avatar
Discard
Best Answer

Hello Bastein,

You need to pass the context here instead of '_default':

    e.g. 'context': {},

And in context you need to pass fields which you want to set defaults with 'default_' prefix.

in your case its like 

    'context': {'default_partners': [(6,0, active_ids)]}, 

Avatar
Discard
Author

Hi ! Thank you for the answer

I got the error : NameError: global name 'active_id' is not defined

when I put it between '' no mistakes but doesn't work.

sorry, my bad. Try using self.env.context.get('active_ids') instead of active_ids.

Author

Still doesn't work, i tried with self._context.get('active_ids') as well,replacing active_ids by active_id etc ...

Do I need to pass values trough context of my tree view ?

Yeah you need to pass that active_id in the context when clicking on the action in 'More' dropdown. which will be propagated to the "New" button action's context.

Author

Is it automatically propagated ? because as i said in my original post, in the url the active_id on my second screenshot is the products i want to match with my label, but in the third screenshot the active_id is the label itself. so i'm scared that i can't use the active_id of the product anymore at this point.

Author

Ok got some news :

def new_match(self):

context=self._context.copy()

if context.get('active_ids'):

context.update(default_products=[(6,0,context.get('active_ids'))])

else:

context.update(default_products=[(6,0,context.get('active_id'))])

return {

'name':_("Save a new matching"),

'view_mode': 'form',

'view_type': 'form',

'view_id': False,

'res_model': 'label.label',

'res_id': self.id,

'type': 'ir.actions.act_window',

'target': 'current',

'domain': '[]',

'context': context,

'flags': {'initial_mode': 'edit',},

}

with this action button, I manage to get the active_ids of my products in the 3rd screenshot. But still nothing filled where it should be. :)

Thank you again !