콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다

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.  

아바타
취소
베스트 답변

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


아바타
취소
작성자

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)]

작성자

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

작성자

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)))

관련 게시물 답글 화면 활동
0
7월 22
1586
0
5월 19
4171
0
8월 20
2243
1
4월 20
7442
2
1월 20
3771