3
votes

Considering the following objects and a corresponding view:

class first_object(osv.osv):
    _name = "first.object"
    _columns = {
                'id': fields.integer ('First ID'),
                'flag': fields.boolean ('Flag'),
                'second_object_id': fields.one2many('second.object','first_object_id')
    }

class second_object(osv.osv):
    _name = "second.object"
    _columns = {
                'id': fields.integer ('Second ID'),
                'first_object_id': fields.many2one('first.object','random field'),
                'field_x': fields.float('x',size=128),
                'field_y': fields.float('y',size=128),
    }

<record model="ir.ui.view" id="first_object_view_id">
    <field name="name">Frist Object</field>
    <field name="model">first.object</field>
    <field name="type">form</field>
    <field name="arch" type="xml">
        <form>
            <notebook>
                <page>
                    <field name="id"></field>
                    <field name="flag"></field>
                    <field name="second_object_id">
                        <tree editable="top">
                            <field name="field_x" attrs="{'invisible':[('flag','=',True)]}"/>
                            <field name="field_y"/>
                        </tree>
                        <form>
                            <field name="field_x"/>
                            <field name="field_y"/>
                        </form>
                    </field>
                </page>
            </notebook>
        </form>
    </field>
</record>

Notice the attrs I have now for the field of the second object named field_x in the tree which is based on the field of the first object named flag.

First of all, the attribute in this case is ignored completely. I dont know why it wont work. Second, assuming this can't work and the attributes MUST refer to local fields, the invisible attribute does not work for the tree view, just the form view. However, if you set a simple invisible="1" in the tree it would work just fine (I cant rely on that, I need the rule I provide with attributes). Any ideas?

EDIT:
The problem seems to be making fields invisible via attributes (and not invisible="1") in the TREE view. It works fine in the form. If this can be done it would solve my problem.

EDIT 2:
I tried with separated view definitions and local fields instead of many2one and one2many to no avail. However, I managed to somehow achieve this with invisible="context.get('xxx',True/False)". The problem is once the condition is matched, it remains invisible even after creating a new record where the condition is not matched.

5
As i know it work on tree view but whole field is not invisible but only that record on which your domain is true is invisible.let's take example if you have 10 record with different state (draft,done) now you put a domain on field invoice reference attrs="{'invisible':[('state','!=','done')]}" so only perticular record which are in done state are not visible but the field name invoice reference is still appear at top if you want to make whole field invisible you shold know there is no sense to put dynamic domain because it affect other record in a tree view.Heroic
thanks Heroic, but actually i already said that this isnt working. keep in mind this isnt a regular tree view, it's a form with in which the one2many field is expanded to a tree and form view, and the attrs are not working in this case3a2roub

5 Answers

1
votes

please look in stock_move_tree from stock.move

<field name="prodlot_id" groups="base.group_extended"/>
<button name="%(track_line)d" string="Split in production lots" type="action"
   icon="terp-stock_effects-object-colorize" attrs="{'invisible': [('prodlot_id','&lt;&gt;',False)]}"
   states="draft,waiting,confirmed,assigned,done"
   groups="base.group_extended"/>
<field groups="base.group_extended" name="tracking_id"/>
<button name="setlast_tracking" string="Put in current pack" type="object"
   groups="base.group_extended"
   icon="terp-stock_effects-object-colorize" attrs="{'invisible': [('tracking_id','&lt;&gt;',False)]}"
   states="draft,assigned,confirmed,done"/>

Is the same solution, but for button, not for regular field. And yes, remove field but show empty column.

1
votes

it seems trying to set an conditional invisible attribute will not affect the true view. only invisible="1". which makes sense since i can't imagine a tree view with some invisible field of which the entire column itself is not invisible.

0
votes

add a related field to flag in second.oject

class second_object(osv.osv):
_name = "second.object"
_columns = {
            'id': fields.integer ('Second ID'),
            'flag': fields.related('first_object_id', 'flag', type='boolean',  relation='first.object', string='Flag'),
            'first_object_id': fields.many2one('first.object','random field'),
            'field_x': fields.float('x',size=128),
            'field_y': fields.float('y',size=128),
}

an then add field flag in your view as invisible and attrs:

<record model="ir.ui.view" id="first_object_view_id">
<field name="name">Frist Object</field>
<field name="model">first.object</field>
<field name="type">form</field>
<field name="arch" type="xml">
    <form>
        <notebook>
            <page>
                <field name="id"></field>
                <field name="flag"></field>
                <field name="second_object_id">
                    <tree editable="top">
                        <field name="flag" invisible="1"/>
                        <field name="field_x" attrs="{'invisible':[('flag','=',True)]}"/>
                        <field name="field_y"/>
                    </tree>
                    <form>
                        <field name="field_x"/>
                        <field name="field_y"/>
                    </form>
                </field>
            </page>
        </notebook>
    </form>
</field>

0
votes

You have take 1 extra new field (i.e boolean type field) in Object2.

and create onchnage on "flag" field of object1.

in that onchnage you set-reset the value of this new field according to value of Flag field.

and put attrs on this new_field instead of Flag.

Hope This will help you

0
votes

Please define the view for the model 'second.object' seperately. The same example is in stock_partial_picking.py file inside wizard folder in stock module. please check that. you may need to define a field as user user1888049 told in his answer