context in act_window

Asked by Alberto Garcia (Factor Libre)

If I do an act_window like this:

<act_window domain="[('animal_id','=',active_id)]" id="action_animals_vaccines_tree" name="Vaccines" res_model="vaccine.animals" src_model="animals.animals" context="{'visit_id':active_id}"/>

the context not work.

But if I do with the typical stucture:

<record model="ir.actions.act_window" id="action_vaccine_vaccine_animals_pendient_tree">
   <field name="type">ir.actions.act_window</field>
   <field name="res_model">vaccine.animals</field>
   <field name="view_mode">tree,form</field>
   <field name="view_type">form</field>
   <field name="domain">[('state','=','pendient')]</field>
                        <field name="context">{'visit_id':active_id}</field>
  </record>

This work. Why?

Question information

Language:
English Edit question
Status:
Solved
For:
Odoo Addons (MOVED TO GITHUB) Edit question
Assignee:
No assignee Edit question
Solved by:
Jay Vora (Serpent Consulting Services)
Solved:
Last query:
Last reply:
Revision history for this message
Launchpad Janitor (janitor) said :
#1

This question was expired because it remained in the 'Open' state without activity for the last 15 days.

Revision history for this message
Alberto Garcia (Factor Libre) (agarcia-flibre) said :
#2

I still need answer

Revision history for this message
Jay Vora (Serpent Consulting Services) (jayvora) said :
#3

Hello Alberto,

The context passed in <record> tag is used as a key to perform some separate operation.

Example :
Going with your example,
<field name="context">{'visit_id':active_id}</field> means, when the action is clicked , the search method of vaccine.animals will have this context.

Now you can perform any operation acc. to the context you passed.

In both the examples, context is passed and works clearly.

As far as the <act_window> is concerned, context is used here for 2 purposes.
1. Definately search based on some specific criteria, by overriding the search of the target model
2. setting the default value to a field,when a new entry is created.

so .

<act_window domain="[('animal_id','=',active_id)]" id="action_animals_vaccines_tree" name="Vaccines" res_model="vaccine.animals" src_model="animals.animals" context="{'default_name':'VAccinated animals'}"/>

Thus, When VAccines is clicked from animals. animals, you will; see some(or 0) records of vaccine.animals.

Clicking on new will show you the effect of context.

I hope this will ease your operational aim.

Thank you.

Revision history for this message
Alberto Garcia (Factor Libre) (agarcia-flibre) said :
#4

This is the problem.

 if you do with
<field name="context">{'visit_id':active_id}</field>
 the default value get from context,
 but in the case of act_window gon't take the default value when you do a new record

Revision history for this message
Launchpad Janitor (janitor) said :
#5

This question was expired because it remained in the 'Open' state without activity for the last 15 days.

Revision history for this message
Alberto Garcia (Factor Libre) (agarcia-flibre) said :
#6

I still need help for this

Revision history for this message
Jay Vora (Serpent Consulting Services) (jayvora) said :
#7

Hello Alberto,

Context behaves same with both the actions.

The reason why context with <field name="context">{'x':y}</field> works must be, the <record> has a domain.

Do you really get active_id while using <record> tag?

default_fieldname works with both the cases.

Thank you.

Revision history for this message
Alberto Garcia (Factor Libre) (agarcia-flibre) said :
#8

<act_window domain="[('animal_id','=',animal_id)]" id="action_visits_analytical_tree" name="Analytics" res_model="analytical.analytical" src_model="visit.visit" context="{'visit_id':active_id}"/>

this not work.

With this act windows when you create a new record get the default value for the animal but not for the visit.

Revision history for this message
Best Jay Vora (Serpent Consulting Services) (jayvora) said :
#9

Yes Alberto,

That's what I say.

To put a value by default on a new record after clicking this act_window, you SHOULD have written {'default_visit_id':active_id}.

In any action , either via <record> or <act_window> , context="{'visit_id':active_id}" will only be used in search() method.

If you want default anme in any case, Use 'default_xyz'.

Again, Do you really get active_id while using <record> tag?
Thanks.

Revision history for this message
Alberto Garcia (Factor Libre) (agarcia-flibre) said :
#10

Thanks Jay (Open ERP), that solved my question.