Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

Conditional field domain - how? [PARTLY SOLVED]

By
zbik
on 2/6/15, 4:02 PM 1,866 views

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

 

Do an onchange_xxx, which will return domain of yyy_ids.

Currenda Sp. z o.o., Mariusz Mizgier
on 2/9/15, 4:13 PM

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

zbik
on 2/9/15, 4:33 PM
-1

Atchuthan - Technical Consultant, Sodexis Inc

--Atchuthan - Technical Consultant, Sodexis Inc--
3883
| 5 3 8
Puducherry, India
--Atchuthan - Technical Consultant, Sodexis Inc--
Passionate coder in Python/Odoo(formerly known as OpenERP).

Contact me via GMail: atchuthantu@gmail.com via Skype: atchuthan_skype
via Linkedin: http://in.linkedin.com/in/atchuthantu
Atchuthan - Technical Consultant, Sodexis Inc
On 2/10/15, 6:00 AM

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

@atchuthan, Thank you, it works :)

zbik
on 2/10/15, 6:29 AM

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

zbik
on 2/10/15, 8:03 AM
0

Ivan

--Ivan--
3210
| 5 3 6
Jakarta, Indonesia
--Ivan--
Ivan
On 2/10/15, 5:53 AM

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

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

zbik
on 2/10/15, 6:00 AM

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

Ivan
on 2/10/15, 6:28 AM

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.

zbik
on 2/10/15, 6:45 AM

Yes. Sorry I did not notice that earlier.

Ivan
on 2/10/15, 6:58 AM
0

deep

--deep--
1617
| 5 4 7
Bengaluru, India
--deep--

Techno-Functional Associate with 6+ years of experience in Odoo (formerly known as OpenERP).

deep
On 2/11/15, 7:15 AM

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,..

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

zbik
on 2/11/15, 11:15 AM

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

deep
on 2/12/15, 12:58 AM

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

deep
on 2/12/15, 1:00 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

2 follower(s)

Stats

Asked: 2/6/15, 4:02 PM
Seen: 1866 times
Last updated: 3/16/15, 8:10 AM