Passa al contenuto
Menu
È necessario essere registrati per interagire con la community.
La domanda è stata contrassegnata
1 Rispondi
6172 Visualizzazioni

I've got a custom module that has a Many2one field on 'product.product'. I would like to extend 'res.users' with a Mayn2Many relation to 'product.product'. 

Is it in some way possible to filter the Many2one relation field 'product.product' in the custom module with the products that are selected in the Many2many field on the user that is logged in. 

And this cannot be done in python with onchange api that then gets the ids and returns it in domain. I don't have any field that I can use the on change except the one for products that I want to filter.  

Avatar
Abbandona
Risposta migliore

You can use m2m field as a domain in m2o field.

Example:

<feld name="product_id" domain="[('id', 'in', product_ids)]"
<field name="product_ids"/>

Let me now if you mean by something else. This will show you only products in m2o field which are selected in m2m field.

You can set the domain dynamically using fields_view_get method.

from lxml import etree

@api.model
def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False):
res = super(class_name, self).fields_view_get(view_id, view_type, toolbar=toolbar, submenu=submenu)
doc = etree.XML(res['arch'])
for node in doc.xpath("//field[@name='product_id']"):
node.set('domain', "[('id', 'in', product_ids)]")
res['arch'] = etree.tostring(doc)
return res


Avatar
Abbandona
Autore

nope this is not what I need. In this example product_ids is on the same model. I need a way to get them from the active/logged in user. Like domain="[('id', 'in', [user.products_ids.ids])]" but it says that there is no user and no I can't have a user_id relation in the model, else I could do it with api.onchange.

Ah ok. Still you can do it by fields_view_get method. Just override this method and add a domain in the field. In the method you will be able to find the products of the logged in user.

product_ids = self.env.user.product_ids.ids

domain = [('id', 'in', product_ids)]

Autore

hm... I never used this fields_view_get method, so I'll have to google a bit how to use it. So this forces the domain on the field? So this means that the filter will be active no mater the view? Because if yes I cannot use it, because I need it only in the form view in the list view I need all the records. Can define it only specific xml_id of view?

See my updated answers. You can do it for specific view as well. You can use "view_id" to add the domain on specific view:

self.env('module_name.form_view_xml_id').id == view_id

Autore

mate thanks... needed a bit to figure out the method. Your example helped. Just needed to change/add

product_ids = self.env.user.products_ids.ids

node.set('domain', "[('id', 'in', {})]".format(tuple(product_ids)))

Post correlati Risposte Visualizzazioni Attività
0
lug 22
1581
0
mag 19
4157
0
ago 20
2238
1
apr 20
7430
2
gen 20
3760