openerp-india team mailing list archive
-
openerp-india team
-
Mailing list archive
-
Message #15316
[Bug 628767] Re: Opening form view from act_window broken
Hello,
This bug is invalid because what you want to achieve is perfectly doable. The part you are missing is the fact that the dynamic definition of an ir.actions.act_window may contain an extra 'res_id' key, which combined with view_mode="form" will directly open the form view of the given record. As of v7.0 the res_id column is explicitly defined on ir.actions.act_window. Before v7.0 this key was only supported in dynamic action definitions, such as those returned by a method call.
Based on the above, and assuming you have a many2one field
'opportunity_id' on the sale.order model, there are many different ways
to directly open the form view of the related opportunity from the
sale.order. Here are a few options:
1. The simplest way does not even use an action and does not need the 'res_id' trick at all. You simply add the opportunity_id field in the form view of sale.order, and make sure to pass a special context to choose the "Opportunity" form view instead of the default "Lead" form. This is as simple as adding the following field to the sale.order form view:
<field name="opportunity_id" context="{'form_view_ref': 'crm.crm_case_form_view_oppor'}"/>
Users can now open the form view of the opportunity from the sale.order directly.
2. As the 'res_id' value of the action will be dynamic, we can't simply create a static 'act_window' action in the database. But we can instead add a new method to sale.order that will return the appropriate action. An example method would be:
def open_opportunity(self, cr, uid, ids, context=None):
return {
'type': 'ir.actions.act_window',
'res_model': 'crm.lead',
'res_id': self.browse(cr, uid, ids[0], context).opportunity_id.id,
'view_mode': 'form',
'view_type': 'form',
'view_id': self.pool.get('ir.model.data').get_object(cr, uid, 'crm','crm_case_form_view_oppor').id,
}
And can be executed by adding a button in the form view of the sale.order, as follows:
<button type="object" name="open_opportunity" string="Open Opportunity"/>
This achieves the same result.
3. If you need the action to be present in the sidebar, you can't directly an <act_window> that will call a python method, so you will need to go through an ir.actions.server that will accomplish the same thing. You can make the server action call the 'open_opportunity' method from example 2 (or move the code of the method in the server action altogether).
The XML definition for the server action and the 'ir.values' entry to bind it to the sidebar could look like this:
<!-- Define the server action that returns the special action definition -->
<record model="ir.actions.server" id="open_related_opportunity">
<field name="type">ir.actions.server</field>
<field name="name">Related Opportunity</field>
<field name="state">code</field>
<field name="model_id" ref="sale.model_sale_order"/>
<field name="code">
# Set the value of the `action` variable to return a custom action to
# be executed by the client. 'object' is initialized based on the 'active_id' in the context
action = object.open_opportunity() # based on method in example 2
</field>
</record>
<!-- Bind the server action as a sidebar button, as <act_window/> will
only do it for ir.actions.act_window actions, not server ones. -->
<record model="ir.values" id="open_related_opportunity_sidebar_binding">
<field name="name">Related Opportunity</field>
<field name="model">sale.order</field>
<field name="key2">client_action_multi</field>
<field name="value" eval="'ir.actions.server,%d' % open_related_opportunity"/>
</record>
Examples 2 and 3 are a bit complex because the 'res_id' value needs to be computed at runtime, so a static <act_window> definition as in the bug description would not work (and this field is not evaluated automatically like `domain`). Nevertheless the mechanisms used are pretty straightforward once you understand them.
Have a look at the source code of official addons to see more examples of ir.actions.server and ir.values use.
** Changed in: openobject-server
Status: New => Invalid
--
You received this bug notification because you are a member of OpenERP
Indian Team, which is subscribed to OpenERP Server.
https://bugs.launchpad.net/bugs/628767
Title:
Opening form view from act_window broken
Status in OpenERP Server:
Invalid
Bug description:
With the XML below, I get a list view containing a single opportunity
when I click on the 'Opportunity' button. If I click on the 'Form'
button in the GTK client, then I get the form view for that single
opportunity. All is well.
If, however, I change the sequence fields so that the form view comes
before the list view and then click the 'Opportunity' button, I get an
empty Opportunity form. If I click on the List button in the GTK
client, I get the correct list, as above. I can then click the Form
button, as above, and get to the expected form view.
I would expect to be able to load the form view straight off, without
the indirection via the list view.
<?xml version="1.0"?>
<openerp>
<data>
<act_window
id="action_opportunities_from_quotes"
name="Opportunity"
res_model="crm.lead"
src_model="sale.order"
view_mode="form"
domain="[('id','=', opportunity_id)]"
view_type="form" />
<record id="action_opportunities_from_quotes_view1" model="ir.actions.act_window.view">
<field name="act_window_id" ref="action_opportunities_from_quotes" />
<field eval="0" name="multi" />
<field eval=""""form"""" name="view_mode" />
<field name="view_id" ref="crm.crm_case_form_view_oppor" />
<field eval="1" name="sequence" />
</record>
<record id="action_opportunities_from_quotes_view2" model="ir.actions.act_window.view">
<field name="act_window_id" ref="action_opportunities_from_quotes" />
<field eval="0" name="multi" />
<field eval=""""tree"""" name="view_mode" />
<field name="view_id" ref="crm.crm_case_tree_view_oppor" />
<field eval="0" name="sequence" />
</record>
</data>
</openerp>
To manage notifications about this bug go to:
https://bugs.launchpad.net/openobject-server/+bug/628767/+subscriptions