Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
1 Odpowiedz
5398 Widoki

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.

Awatar
Odrzuć
Najlepsza odpowiedź

Hi, my code field_view_get change readonly attr work

https://github.com/shurshilov/Tabel/blob/8.0/models.py#L592
Awatar
Odrzuć
Autor

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

Powiązane posty Odpowiedzi Widoki Czynność
0
mar 25
1430
0
sty 25
3526
1
sie 23
14878
change password Rozwiązane
1
sie 23
13541
1
lip 23
10516