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

Dynamic placeholder: different placeholders for same view shown in different menus

By
Anton Chepurov
on 10/1/15, 8:26 AM 1,209 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.


1

Temur

--Temur--
2894
| 6 5 7
Tbilisi, Georgia
--Temur--
Programmer
Temur
On 10/1/15, 9:04 AM

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" />

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

Anton Chepurov
on 10/1/15, 3:54 PM

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

Temur
on 10/2/15, 5:46 AM

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

Anton Chepurov
on 10/2/15, 8:03 AM

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.

Temur
on 10/2/15, 10:31 AM

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

Temur
on 10/2/15, 10:58 AM

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

Anton Chepurov
on 10/2/15, 3:54 PM

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

Temur
on 10/5/15, 7:16 AM
1
Anton Chepurov
On 10/1/15, 3:49 PM

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

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: 10/1/15, 8:26 AM
Seen: 1209 times
Last updated: 10/6/15, 12:31 PM