fueggit fueggit - 1 year ago 72
Python Question

Odoo hide field by state in xml

class myModel(models.Model):

_name = 'my.model'

state = fields.Selection(selection=_STATES, string='Status', index=True, track_visibility='onchange',required=True, copy=False, default='draft')
my_model_line = fields.One2many('my.model.line', 'model_id')


another model:

class MyModelLine(models.Model):

_name = 'my.model.line'

name = fields.Char(string='Name')
quantity = fields.Integer(string='Quantity', required=True, default=1)
remarks = fields.Text(string='Description')
my_model_id = fields.Many2one('my.model', 'My Model')


my_model.xml

<field name="my_model_line" attrs="{'readonly': [('state','not in', ('draft'))]}">
<tree string="My model Lines" editable="bottom">
<field name="name"/>
<field name="quantity"/>
<field name="remarks"/>
<button name="open_new_view" type="object" string="Add" class="oe_highlight"/>
<button icon="terp-face-plain" name="test" type="object" string="Add" class="oe_highlight"/>
</tree>
</field>


I want to hide my.model.line moel remarks field and Add button field depend on my.model status.
For example hide fields if is my model status is approved.

As I understand, I can't use invisible attribute with domain in my my_model.xml because my.model.line do not have state field. Maybe there are any solutions to do that?

I was thinking to create status field in my.model.line object ant change it status depend on my.model status.

Also I was trying like this:

<field name="remarks" attrs="{'invisible': "[('my_model_id .state', '=', 'approved')]"}"/>


But then I got an error: "Unknown field my_model_id.state in domain"

Answer Source

First to use field in attrs this field should be added to the form view it's selt not just the model keep this in mind even if it means add this field and make it invisible.

Second if you want to show a field of other module that have a many2one field to it use related field.

         state = fields.Selection( 'put same selzction here too',  related='many2one_field.state's, readonly=True) 

Related field Must have the same type if the field is char use char, if the field is integer use integer, if the field is many2one use many2one. For most field just attribute related is enough but selection redefine the same selection.

And now you can use the field like it's a field of the current model.

Soryy about my english hope you get the idea

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download