This question has been flagged
3 Replies
17945 Views

I have seen variables called from context this way:

context="{'template': template_value,}"

They're coming from some value of the current form, aren't they? And, why can't I pass it through an act_window to the destination model. For example, in the context of an act_window, if I write this:

context="{'default_template_id': 2,}"

It works, but not if I try to get the context of the source model:

context="{'default_template_id': context.get('template_id', False),}"

Another option would be to call a function from there, but I think that's not possible:

context="{'default_template_id': my_function,}"

Can anyone help me? Thank you in advance.

EDIT

The only way I know to add an option to the menu More in tree views is through XML (how can I add an option to More in Python?), so my act_window is defined in XML:

<act_window name="Invite partners to an event"
    res_model="mail.compose.message"
    src_model="event.registration"
    view_mode="form"
    multi="True"
    target="new"
    context="{'default_template_id': context.get('template_id', False),}"
    id="invite_partner_to_event"/>

Avatar
Discard
Best Answer

(After rewriting my answer several times)

If you are assuming that the context contains all field values of your current view, you are wrong.

But you can easily add values from the fields of your model to the context .

context = "{'default_template_id': template_id}"

 

EDIT:

John Does solution is a quick work arround.

Another suggestion is to add your values with the right key in the previous view (unless there is no name collision). This way you don't have to change the context. It should be passed unchanged.

And finally, after some research, what seems ugly but is powerful:

  1. Create a server action (ir.act.server) that executes python code
  2. Bind this server action to your object as multi_action via an action_binding (ir.values)
  3. Let your server action return a window action (ir.act.act_window)
    1. either as python dictionary directly in the server action
      action = { ..... }
    2. or as python code in a method of the related object
      action = obj.my_client_action_multi_function(context=context)
    3. (or maybe even as window action created in xml?)

Compare the answer of Daniel Reis in this SO topic:
http://stackoverflow.com/questions/11668636/openerp-action-to-call-a-method-on-all-ids-selected-in-a-tree-view

If you choose 3.2 your function could look like this:

def my_client_action_multi_function(cr, uid, ids, context=None):
if context is None:
    context = {};
context['default_template_id'] = context.get('template_id',False);
return {
            'name': ...,
            'res_model': ...,
            'type': 'ir.actions.act_window',
            'view_mode': ...,
            'view_type': ...,
            'domain': [],
            'context': context,
}

In all cases, you can directly manipulate your context in the server action before returning the window action.

 

Regards.

Avatar
Discard
Author

Thank you René! Yes, I thought that, so I was wrong. I passed some variables to a view through context, and in this view I declared an act_window (to see a new option in "More...") which redirects me to other model view. In that act_window, I try to get the context I was using, but it's not recognized.

Updated my answer, but can you please post your code?

Author

Thank you René, I edited my post with the code.

Best Answer

One workaround that came into my mind is to create a "dummy" field to store the content of the context.get('template_id', False) (invisible, if you want to), and then use that "dummy" field to feed the other context.

Avatar
Discard
Best Answer

Hi,

Template is nothing predefined views with values. This templates are created and stored in database. When you create new record templates are sent to new form via context. You could use onchange function http://stackoverflow.com/questions/22218067/openerp-onchange-method-for-fields-in-different-page-of-view

Avatar
Discard
Author

Thank you George for your attention. But I have re-read my own question and it's very confusing, I haven't explained it very well and that's not what I was refering to. I'm going to edit to enlight it better.