Siirry sisältöön
Menu
Sinun on rekisteröidyttävä, jotta voit olla vuorovaikutuksessa yhteisön kanssa.
Tämä kysymys on merkitty
1 Vastaa
5368 Näkymät

Hello,

I want to make many2one field readonly=false which is located in one2many field and this field follow the attribute readonly=true from the py file.


Here are defined invoice_line field in base module.

invoice_line = fields.One2many('account.invoice.line', 'invoice_id', string='Invoice Lines', readonly=True, states={'draft': [('readonly', False)]}, copy=True)


What i wan't is in this invoice_line field there is one m2o field is name which is description simply i want to make that field readonly=false whichever state follow the invoice. Below i just try the fields_view_get but didn't get any successful result.

 @api.model

    def fields_view_get(self, view_id=None, view_type='tree', toolbar=False, submenu=False):

#         context = if self.env.context is None:context = {}

        res = super(AccountInvoice, self).fields_view_get(view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=False)

        active_id = self.env['account.invoice'].browse(self.env.context.get('active_id'))

        print "-----------------",active_id.state

        if view_type == 'form':

            fields = res.get('fields', {})

            method_nodes = etree.XML(res['fields']['invoice_line']['views']['tree']['arch'])

            for node in method_nodes.xpath("//field"):

                modifiers = json.loads(node.get("modifiers", '{}'))

                modifiers.update({'readonly':True})

                node.set("modifiers", json.dumps(modifiers))

            for node in method_nodes.xpath("//field[@name='name']"):

                modifiers = json.loads(node.get("modifiers", '{}'))

                modifiers.update({'readonly': False})

                node.set("modifiers", json.dumps(modifiers))

            res['fields']['invoice_line']['views']['tree']['arch'] = etree.tostring(method_nodes)

        return res

 Please give some suggestion.

Thanks in advance.

Avatar
Hylkää
Paras vastaus

Hi, my code field_view_get change readonly attr work

https://github.com/shurshilov/Tabel/blob/8.0/models.py#L592
Avatar
Hylkää
Tekijä

Hii Shurshilov

Thank you for your efforts.

But it is not work for me because in your case you give attrs on those fields which is integer.

In my case i want to make attrs based on state. If state in open or paid the only one m2o field can readonly instead of all fields under One2many field.

Issue is we didn't find the state value on the fly-way.

Any other way to solve this problem.

you must manually inherit the view and change the field one2many to tree

<field name="invoice_line">

<tree editable="bottom">

<field name="name" readonly= "False"/>

</tree>

in my code

https://github.com/shurshilov/Tabel/blob/8.0/views/tabel.xml#L365

Aiheeseen liittyviä artikkeleita Vastaukset Näkymät Toimenpide
0
maalisk. 25
1427
0
tammik. 25
3510
1
elok. 23
14873
1
elok. 23
13525
1
heinäk. 23
10502