Reputation: 10189
I have a One2many field, which is showing the tree view with the highest priority of its comodel. Actually I want to show that tree view, but with create mode disabled (to avoid users to add items).
So I was wondering if there is any way to do that without calling a replica of the whole tree view only to add the attribute create="false"
to the tree
tag.
I think that adding another record to the ir_ui_view
only for that is a bit repetitive and not very functional.
Does anyone use a better method?
EDIT
I am editing this to be clearer since every answer is proposing the same. The following code is not a solution for my question:
<field name="one2many_field">
<tree create="false">
<field name="field_x"/>
<field name="field_y"/>
...
</tree>
</field>
In this case I have to copy all the fields of the existing tree view into the tree
tag. I am trying to avoid that for several reasons:
depends
parameter of the __manifest__.py
of my custom module to include all the modules which insert those fields. However, my module does not depend on those modules at all, I would have to do that only to replicate the tree view.create="false"
).So I ask if someone knows a way to use an existing view (not paste its fields inside a tree
tag) but disabling its Create option.
Upvotes: 2
Views: 2492
Reputation: 397
Interesting question. I actually didn't know if this could be done, but you can achieve it with a combination of tree_view_ref
, view mode
and priority
.
I created a couple of simple addons (custom_sale
and test_addon
) to test this. The first one just adds a new field to one of the sale.order
tree views. I did this to make sure the other module shows the new field too in the One2many
.
sale_order.py
from odoo import fields, models
class SaleOrder(models.Model):
_inherit = 'sale.order'
dummy_field = fields.Char(String="Dummy")
sale_order_views.xml
<odoo>
<record id="view_quotation_tree_inherit" model="ir.ui.view">
<field name="name">view_quotation_tree_inherit</field>
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale.view_quotation_tree"/>
<field name="arch" type="xml">
<field name="state" position="after">
<field name="dummy_field"/>
</field>
</field>
</record>
</odoo>
dummy_model.py
from odoo import fields, models
class Dummy(models.Model):
_name = 'dummy.model'
name = fields.Char()
order_ids = fields.One2many(
comodel_name='sale.order',
inverse_name='dummy_id'
)
sale_order.py
from odoo import fields, models
class Order(models.Model):
_inherit = 'sale.order'
dummy_id = fields.Many2one(comodel_name='dummy.model')
Nothing fancy so far. All the magic happens on the views. First, we have to inherit the tree view of the model our One2many
field points to: in my case, sale.order
. We have to make sure the new view's mode
is primary
. And we set a high enough priority to ensure that the fields added by other modules we don't depend on also appear in our view (I think priority can go as high as 9999, not sure though). Of course, we also add the create="false"
attribute to the tree
node.
sale_order_views.xml
<odoo>
<record id="view_quotation_tree_inherit" model="ir.ui.view">
<field name="name">view_quotation_tree_inherit</field>
<field name="model">sale.order</field>
<field name="mode">primary</field>
<field name="priority" eval="500"/>
<field name="inherit_id" ref="sale.view_quotation_tree"/>
<field name="arch" type="xml">
<tree position="attributes">
<attribute name="create">0</attribute>
</tree>
</field>
</record>
</odoo>
And, on the dummy.model
form view, when we declare the order_ids
field, we have to pass our view via context with tree_view_ref
key (you might also want to check form_view_ref
). I also added a simple action and some menu items so I could test everything.
dummy_model_views.xml
<record id="dummy_view_tree" model="ir.ui.view">
<field name="name">dummy_view_tree</field>
<field name="model">dummy.model</field>
<field name="arch" type="xml">
<tree>
<field name="name"/>
</tree>
</field>
</record>
<record id="dummy_view_form" model="ir.ui.view">
<field name="name">dummy_view_form</field>
<field name="model">dummy.model</field>
<field name="arch" type="xml">
<form>
<group>
<field name="name"/>
</group>
<field name="order_ids"
context="{'tree_view_ref': 'test_addon.view_quotation_tree_inherit'}"/>
</form>
</field>
</record>
<record model="ir.actions.act_window" id="action_dummy_tree">
<field name="name">Dummy List</field>
<field name="res_model">dummy.model</field>
<field name="view_mode">tree,form</field>
<field name="context">{}</field>
</record>
<menuitem name="Dummy" id="menu_dummy" sequence="70"/>
<menuitem name="Dummy List" id="menu_dummy_list" sequence="5" parent="menu_dummy" action="action_dummy_tree"/>
</odoo>
Note that, in my test_addon
, I did not have any way to fill the O2m field (I guess you are doing it programatically or that they are created in some other way), so I enabled and disabled creation on the tree view in order to create some records and test if I was able to edit and delete them. By the way, I'm quite sure this should also work for Many2many
fields.
Hope it helps.
Upvotes: 4
Reputation: 686
Try this . It will disable the button
<tree string="Sim Tree" create="false" >
Upvotes: 0
Reputation: 1068
The readonly="1"
attribute applied to the One2many field.
It's used on the view view_move_form
on <path_to_v12>/addons/stock/views/stock_move_views.xml
like so:
<field name="move_orig_ids" string="Origin Moves" readonly="1">
<tree>
<field name="location_id"/>
<field name="location_dest_id"/>
<field name="product_uom_qty"/>
<field name="product_uom"/>
<field name="state"/>
</tree>
</field>
So, maybe <field name="one2many_field_ids" readonly="1"/>
will suffice.
Upvotes: 0
Reputation: 1068
The create="false"
attribute on the <tree>
tag will do the trick, so why don't you inherit your view and change the attributes of the <tree>
tag.
<template id="your_view_id" inherit_id="some_module.some_view_id" name="Your View" priority="50">
<xpath expr="//tree" position="attributes">
<attribute name="create" eval="False"/>
</xpath>
</template>
The following example will show a tree view on which the one-to-many relationship is read-only
<field name="one_to_many_field_ids">
<tree create="false">
<field name="name"/>
</tree>
</field>
Upvotes: 0