Skip to Content
Menu
This question has been flagged
2 Replies
10585 Views

How can I inherit a view such that it shows different placeholders when shown from different menus?

It seems that inheritance is not a mechanism that would allow this, am I right?

If so, are there any other ways of achieving the same view but with different placeholders?

Copy-pasting the view wouldn't work as the inherited views won't be considered/pulled in.


A widget, probably?

Ah, I think fields_view_get() should fix the issue :) Will try it out.


Avatar
Discard
Best Answer

You can have different views per menu (you can define explicitly which view should be used on which menu using xml id). Say, for two menu, try to create two views, with different name and xml id,  adapt placehoders in both views, then bind the two views to the corresponding menus. You can also leverage "sequence" field of view in some way.


UPDATE:

(example code is for Odoo 8.0, requires Sales module to be installed )

the following example adds two pages, using different inherited views per menu, as suggested above.

first page: "/Sales/Products/Products AAA"

        <record model="ir.ui.view" id="product_product_aaa_page_view">
<field name="name">aaa.text.product.form.view</field>
<field name="model">product.template</field>
<field name="inherit_id" ref="product.product_template_only_form_view" />
<field name="mode">primary</field>
<field name="arch" type="xml">
<field name="description" position="attributes">
<attribute name="placeholder">You are on "/Sales/Products/Products AAA" page!</attribute>
</field>
</field>
</record>


<record id="product_action_aaa" model="ir.actions.act_window">
<field name="name">Product AAA</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">product.template</field>
<field name="view_mode">kanban,tree,form</field>
<field name="view_type">form</field>
<field name="view_id" ref="product_product_aaa_page_view"/>
</record>

<menuitem action="product_action_aaa"
id="menu_product_template_aaaa"
parent="base.menu_product" sequence="2" />

and here is second page: "/Sales/Products/Products BBB"

        <record model="ir.ui.view" id="product_product_bbb_page_view">
<field name="name">bbb.text.product.form.view</field>
<field name="model">product.template</field>
<field name="inherit_id" ref="product.product_template_only_form_view" />
<field name="mode">primary</field>
<field name="arch" type="xml">
<field name="description" position="attributes">
<attribute name="placeholder">You are on "/Sales/Products/Products BBB" page!</attribute>
</field>
</field>
</record>

<record id="product_action_bbb" model="ir.actions.act_window">
<field name="name">Product BBB</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">product.template</field>
<field name="view_mode">kanban,tree,form</field>
<field name="view_type">form</field>
<field name="view_id" ref="product_product_bbb_page_view"/>
</record>

<menuitem action="product_action_bbb"
id="menu_product_template_bbbb"
parent="base.menu_product" sequence="3" />

Avatar
Discard
Author

I'm sorry, but you didn't bother reading the question, did you? Nah, this happens more often than not on this forum... :/

You're welcome. There is always several ways to achieve the same thing, so it's normal to get alternatives either on this forum or elsewhere...

Author

@Temur, it was too rude of me, I'm sorry. I didn't mean that. I'm really sorry. What I meant was that your answer doesn't solve the described problem - and I've explained why in the question itself: > Copy-pasting the view wouldn't work as the inherited views won't be considered/pulled in. I lost my cool because for almost every question I post here I get an answer which I have beforehand deliberately explained in the question that it doesn't work for the given problem. So was yours, too. So that's not an alternative, as you say. It doesn't achieve what the problem formulates. I'm sorry once again.

Apology accepted. BUT, [1] you have combined multiple (4 or 5) questions in the one, probably my answer is not covers all of them, but I think it answers some of them. [2] I have not to be agree whatever you're explaining in your question, but it does not mean I have not read it! Yes, I read that you explained as if "inherited views won't be considered/pulled in", but I disagree with this assumption and I stated clearly in my answer that you CAN have different views per menu, if you use xml id to bind views to menus. I mentioned also sequence (i.e. priority of view). if you bind view to menu with it's xml id as I suggested, then this view is used, regardless of sequence, otherwise (as in most cases) as we do not bind view to menu, then view is chosen according it's sequence(priority). I do not argue about how good the alternative is, and is it best fit for your case or not. but you can gather some knowledge from it and maybe it'll be best fit in other cases. So, what my message was all about is that answer to your question #2 in the post is NO, and further explanation about how to do #1. and it's clearly an alternative and I thought it should be interesting for you, as YOU ASKED ABOUT IT. I'll add an example code to my answer, you can check it and see yourself answer to your #1 question. BUT, my answer was explaining in words the case, that you can see in the example code, you should not require the example for understanding that.

Example is added. Now you can try it, and see with your eyes different placeholder shown from different menus, using inherited view. BTW, congrats with your own solution and +1. As your problem is solved, it's not necessary to continue further discussion on this thread I think. But you have not to be agree with me, as you like..

Author

Temur, thank you for your time providing me with an example! It is absolutely identical to the first idea that came to my mind -- and I tried it -- and it doesn't work. To be absolutely sure, I've looked up the history of the file in the Local History of IntelliJ, and yes, it's absolutely identical. I've copy-pasted it to my answer (see above). Could you explain where is the difference between your solution and mine and why it doesn't work in my particular case? (always the last overridden view is chosen and shown in both menus regardless of the different view_id-s specified).

<field name="mode">primary</field>
'primary' mode - means more independent view, then default 'extension' mode. Quote from help msg:
* if extension (default), if this view is requested the closest primary view is looked up (via inherit_id), then all views inheriting from it with this view's model are applied
* if primary, the closest primary view is fully resolved (even if it uses a different model than this one), then this view's inheritance specs () are applied, and the result is used as if it were this view's actual arch.

Author Best Answer

Yep, fields_view_get() + context key set in the menu solved that.

Here's the solution.

Context:                        (note the bold part)

        <record id="action_picking_int_form" model="ir.actions.act_window">
<field name="name">Move Items</field>
<field name="res_model">stock.picking</field>
<field name="type">ir.actions.act_window</field>
<field name="view_type">form</field>
<field name="view_mode">form</field>
<field name="view_id" ref="view_picking_form"/>
<field name="target">current</field>
<field name="context">{'picking_type': 'int'}</field>
</record>

And view_get():

    def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
res = super(stock_picking, self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=submenu)
if view_type == 'form':
doc = etree.XML(res['arch'])
note = doc.xpath("//field[@name='note']")

picking_type = context.get('picking_type')
if picking_type == 'in':
note[0].attrib['placeholder'] = 'Model | Serial Number | Model | Serial Number | ...'
elif picking_type == 'int':
note[0].attrib['placeholder'] = 'Location Barcode | Quant ID | Quant ID | Quant ID | ...'

res['arch'] = etree.tostring(doc)
return res


UPDATE:

Why does this solution show the same placeholder for both menus - for Receive New Products and for Move Products?

Both menus show Location | Barcode as a placeholder (taken from the last view definition). If smb asks, <data> tag doesn't have a noupdate flag.

<record id="view_picking_form" model="ir.ui.view">
<field name="name">stock.picking.form</field>
<field name="model">stock.picking</field>
<field name="inherit_id" ref="stock.view_picking_form"/>
<field name="arch" type="xml">
<field name="note" position="replace">
<group col="4">
<field name="note" class="oe_inline" colspan="2" nolabel="1" placeholder="Add an internal note..."/>
<field name="info" style="color: blue;" colspan="2" nolabel="1"/>
</group>
</field>
</field>
</record>
<record id="view_picking_in_form" model="ir.ui.view">
<field name="name">stock.picking.in.form</field>
<field name="model">stock.picking</field>
<field name="inherit_id" ref="view_picking_form"/>
<field name="arch" type="xml">
<field name="note" position="attributes">
<attribute name="placeholder">Model | Serial_Number</attribute>
</field>
</field>
</record>
<record id="view_picking_int_form" model="ir.ui.view">
<field name="name">stock.picking.int.form</field>
<field name="model">stock.picking</field>
<field name="inherit_id" ref="view_picking_form"/>
<field name="arch" type="xml">
<field name="note" position="attributes">
<attribute name="placeholder">Location | Barcode</attribute>
</field>
</field>
</record>

<record id="action_picking_form" model="ir.actions.act_window">
<field name="name">Receive New Products</field>
<field name="res_model">stock.picking</field>
<field name="type">ir.actions.act_window</field>
<field name="view_type">form</field>
<field name="view_mode">form</field>
<field name="view_id" ref="view_picking_in_form"/>
<field name="target">current</field>
<field name="context">{'picking_type': 'in'}</field>
</record>
<menuitem action="action_picking_form" id="menu_action_picking_form" parent="stock.menu_stock_warehouse_mgmt" sequence="3"/>
<record id="action_picking_int_form" model="ir.actions.act_window">
<field name="name">Move Products</field>
<field name="res_model">stock.picking</field>
<field name="type">ir.actions.act_window</field>
<field name="view_type">form</field>
<field name="view_mode">form</field>
<field name="view_id" ref="view_picking_int_form"/>
<field name="target">current</field>
<field name="context">{'picking_type': 'int'}</field>
</record>
<menuitem action="action_picking_int_form" id="menu_action_picking_int_form" parent="stock.menu_stock_warehouse_mgmt" sequence="10"/>

Receive New Items: https://www.dropbox.com/s/uco8b99arlz2qf8/Receive_New_Items.png?dl=0

Move Items: https://www.dropbox.com/s/x706dftvmfieh0n/Move_Items.png?dl=0


If I set priorities for both views (and strictly for both views, just for one is not enough!) like this:

        <record id="view_picking_in_form" model="ir.ui.view">
<field name="name">stock.picking.in.form</field>
<field name="model">stock.picking</field>
<field name="inherit_id" ref="view_picking_form"/>
<field name="priority" eval="21"/>
<field name="arch" type="xml">
<field name="note" position="attributes">
<attribute name="placeholder">Model | Serial_Number</attribute>
</field>
</field>
</record>
<record id="view_picking_int_form" model="ir.ui.view">
<field name="name">stock.picking.int.form</field>
<field name="model">stock.picking</field>
<field name="inherit_id" ref="view_picking_form"/>
<field name="priority" eval="20"/>
<field name="arch" type="xml">
<field name="note" position="attributes">
<attribute name="placeholder">Location | Barcode</attribute>
</field>
</field>
</record>

... then Model | Serial_Number placeholder is shown in both menus.

Hence, it is either a bug in Odoo's view inheritance mechanism, or inheritance simply doesn't allow such tuning of placeholders and hence the only solution known to me so far is overriding the fields_view_get().

Avatar
Discard
Related Posts Replies Views Activity
1
Mar 20
1988
12
Dec 18
32521
1
Jul 16
5608
2
Mar 15
4603
1
Mar 15
2707