Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

Can I pass a variable through an act_window in XML?

By
Juan Formoso
on 11/17/14, 6:32 AM 4,280 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"/>

2

René Schuster

--René Schuster--
1560
| 5 5 8
Weinheim, Germany
--René Schuster--

Challenge Everything!

René Schuster
On 11/18/14, 6:53 AM

(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.

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.

Juan Formoso
on 11/18/14, 7:18 AM

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

René Schuster
on 11/18/14, 7:48 AM

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

Juan Formoso
on 11/18/14, 7:58 AM
1

Ivan

--Ivan--
3210
| 5 3 6
Jakarta, Indonesia
--Ivan--
Ivan
On 11/18/14, 7:09 AM

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.

0
George
On 11/18/14, 6:15 AM

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

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.

Juan Formoso
on 11/18/14, 6:23 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 11/17/14, 6:32 AM
Seen: 4280 times
Last updated: 3/16/15, 8:10 AM