Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
3 Trả lời
20187 Lượt xem

Is it posssible to apply a part domain only when some condition is true otherwise the part domain should not be applied?

For example (extremely simplified this example):

xxx = fields.Boolean('XXX')

<field name="xxx"/>
<field name="yyy_ids" widget="many2many" domain="['|',(False,'=',xxx),(False,'!=',xxx) ]">

Example result: ValueError: Invalid leaf [False, '=', True]

otherwise writing, I need:

if xxx:
    <field name="yyy_ids" widget="many2many"  domain="[...version1....]"/>
else:
    <field name="yyy_ids" widget="many2many"  domain="[...version2....]"/>

 

UPDATE, SOLUTION:

    @api.model    
    def fields_view_get(self, view_id=None, view_type=False, context=None, toolbar=False, submenu=False):
        res = super(my_model, self).fields_view_get(view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=submenu)
        doc = etree.XML(res['arch'])
        nodes = doc.xpath("//field[@name='yyy_ids']")

     
        ### if self.xxx:  ### This solution does not work !!!   
 
        if 'xxx' in self._context:  ### this only works
            for node in nodes:
                node.set('domain', "....version1....")
        else:   
            for node in nodes:
                node.set('domain', "....version2....")
        res['arch'] = etree.tostring(doc)
        return res

 

Ảnh đại diện
Huỷ bỏ

Do an onchange_xxx, which will return domain of yyy_ids.

Tác giả

Thx for right answer. I know but at the start? See my next question: https://www.odoo.com/forum/help-1/question/equivalent-javascript-onload-it-is-possible-when-form-is-loaded-75863

Câu trả lời hay nhất

you could try using fields_view_get to set the domain when FORM is loaded

Ảnh đại diện
Huỷ bỏ
Tác giả

@atchuthan, Thank you, it works :)

Tác giả

but only partly solved :(, with context variable , how to get active_ids?

Câu trả lời hay nhất

Use something like ['I', '&', ('xxx', '=', True), ...version1...., ...version2....].  Care must be taken in the logical operations.  You might want to be more explicit here.

Ảnh đại diện
Huỷ bỏ
Tác giả

This is not a good solution. With ('xxx', '=', True) ValueError: Invalid field 'xxx' in leaf "

The field 'xxx' need to be included in the view as well.

Tác giả

Field xxx is included in view and error exists. As I tested, 'xxx' (used on the left) may be only field from yyy_ids model.

Yes. Sorry I did not notice that earlier.

Câu trả lời hay nhất

You can try that in 2 ways:

1. You can create another field, in that you store the filter value dynamically by calling onchange_event, and use this new field to filter your orginal field...

OR

2. You can have two fields (say "yyy_ids" and  "yyy1_ids" on which static domain is set respectively), then you can enable one of the field based on your criteria,..

Ảnh đại diện
Huỷ bỏ
Tác giả

Thx for right answer. but... way-2 - program code with this solution strongly complicates. way-1 - how set domain at start? without onchange?

you set it in XML... by using context....

provided, the form is loaded freshly, but if the control is stayed in original page then you can't set it...

Bài viết liên quan Trả lời Lượt xem Hoạt động
2
thg 5 24
7831
2
thg 5 24
9239
3
thg 5 24
6039
4
thg 11 23
2812
1
thg 1 22
12000