This question has been flagged

After many attemps and reading a lot here, I was finally able to set the domain of a field dynamically using a function.

I will post my findings here in case it would help anyone.


1) Domain is typically set in XML view like this : 

domain="[('id', 'in', ['1','2','3','4','5'])]"

Here, I provide five ID's manually for the example.


2) Another method to set the same domain is to write a function in your model .py

Example to modify domain depending on "yourfield" :


@api.onchange("yourfield")

def _onchange_all_partner_ids(self):
    res = {}
    res['domain'] = {'partner_id': [('id', '=',   ['1','2','3','4','5'] )], } 
    return res


3) Another method to provide ID's dynamicaly with a function, we can use à "virtual non-stored and computed" many2many field and use it in the view: 

In .py file :

#first function retrieves a list of ID's :

@api.multi
def _get_all_partner_ids(self):
     partner_id_list = [] 

# write some code to retrieve partners in xxxx

     partner_id_list.append( xxxxx.id ) 
     return partner_id_list


#second function is a compute for field all_partner_ids   : 

@api.multi
@api.depends("partner_contact_id")
def _load_all_partner_ids(self):
     self.all_partner_ids = [(6,0, self._get_all_partner_ids())]

#last : define all_partner_ids many2many field

all_partner_ids = fields.Many2many('res.partner',string="All partners",compute="_load_all_partner_ids") 


in .XML

In your form view, you have to add you many2many field like this :

<field name="all_partner_ids" invisible="True"/>


And you finally set domain like this : 

domain="[('id', 'in', all_partner_ids and all_partner_ids[0] and all_partner_ids[0][2] or False)]"



To give a more specific example : 

I wanted to set a domain on field "partner_id" based on a list of parner ID that I would compute in a function.

That list had to be retrieved recursively based on another field : partner_contact_id.


I had to do two things : 

A) I began to set domain with "on_change("partner_contact_id)" compute domain, like this : 

@api.multi
def _get_all_partner_ids(self):
     if not self.partner_contact_id.id:
          partner_id_list = []
     else: 
          partner_id_list = []
          partner_id_obj = self.env['res.partner'].search([('parent_id', '=', self.partner_contact_id.id)])
          for data in partner_id_obj:
               partner_id_list.append(data.id)
               for ids in partner_id_list:
                    childs = self.env['res.partner'].search([('parent_id', '=', ids)])
                         for child in childs:
                         if child.id <> self.partner_contact_id.id and child.id not in partner_id_list:
                              partner_id_list.append(child.id)
     return partner_id_list


@api.onchange("partner_contact_id")
def _onchange_all_partner_ids(self):
     if not self._get_all_partner_ids():
          res = {}
          res['domain'] = {'partner_id': [],
                                    'partner_invoice_id': [],
                                    'partner_shipping_id': []
                                   }
          return res
     else:
          res = {}
          res['domain'] = {'partner_id': [('id', '=', self._get_all_partner_ids())],
     'partner_invoice_id': [('id', '=', self._get_all_partner_ids())],
     'partner_shipping_id': [('id', '=', self._get_all_partner_ids())]
     }
     return res


But when I loaded an old sale order, or when I created a new one, " @api.onchange("partner_contact_id") " was not triggered and my domain was not set.


B)   To solve this, I decided to compute a many2many field with my ID list :

in .py :

@api.multi
@api.depends("partner_contact_id")
def _load_all_partner_ids(self):
     self.all_partner_ids = [(6,0, self._get_all_partner_ids())]

all_partner_ids = fields.Many2many('res.partner',string="All partners",compute="_load_all_partner_ids")


in xml :

<field name="all_partner_ids" invisible="True"/>

<field name="partner_id" position="attributes">
<attribute name="domain">[('id', 'in', all_partner_ids and all_partner_ids[0] and all_partner_ids[0][2] or False)]</attribute>
</field>

Avatar
Discard

Hi men, i was looking for a solution like that for some days right now.

REALLY THANK FOR THE GREAT EXPLATION YOU GIVE!

Author

Thanks for your feedback ! You're welcome !

Best Answer

Hi,

Other than this method, you can use the web domain field module from oca for the same:  Web Domain Field


See this: Return Dynamic Domain For Field In Odoo


How to use:

.. code-block:: xml

<field name="product_id_domain" invisible="1"/>
<field name="product_id" domain="product_id_domain"/>


.. code-block:: python

product_id_domain = fields.Char(
compute="_compute_product_id_domain",
readonly=True,
store=False,
)

@api.multi
@api.depends('name')
def _compute_product_id_domain(self):
for rec in self:
rec.product_id_domain = json.dumps(
[('type', '=', 'product'), ('name', 'like', rec.name)]
)

Returning domain from the onchange function: How To Give Domain For A Field Based On Another Field

Thanks

Avatar
Discard
Best Answer

Hello, Thank you for your extended explanation! great work.

I have been trying your method to filter sale order lines based on the field value of a field in the order-line, but it looks like this method doesn't filters One2many fields?

When I follow your method everything works up until the filtering of the m2m field. I made it visible on my view and could see that it behaved perfectly like I wanted. So filtering the lines each time on update of the parent field.

Do you know if this solution is possible to filter a O2m field? Or do you know how to do this some other way.

Thanks

Avatar
Discard

See my answer about the web domain field module