Domain on selector view of a Many2Many


#1

Hi,
Is there a way to filter the view back by a ‘many2many’ when adding a value (the view that shows the list of possible values).
The ideal would have been to put a ‘domain’ on the call of this view, but I do not know where to place it.
I already trying to put a domain on the field but is too restrictive because when registering the field it would seem that the domain of values already present is also verified.
Or perhaps there is another way ?

Regards.


(Cédric Krier) #2

There is the filter attribute.


#3

Thank you, I did not see this attribute.
Is a PYSON expression valid because I have the following error with the same syntax as a domain:

filter=[(‘company’, ‘=’, Eval(‘context’, {}).get(‘company’, -1))]


File “/trytond/model/fields/field.py”, line 356, in convert_domain
expression = Operator(column, self._domain_value(operator, value))
File “/trytond/model/fields/field.py”, line 333, in _domain_value
return self.sql_format(value)
File “/trytond/model/fields/many2one.py”, line 113, in sql_format
return int(value)
TypeError: int() argument must be a string, a bytes-like object or a number, not ‘Get’


(Cédric Krier) #4

Yes, it is just appended to the domain sent to the client.


#5

But when i put a PYSON expression i have this error :


TypeError: int() argument must be a string, a bytes-like object or a number, not ‘Get’

if i use a ‘Get’ or if i use an ‘Eval’


TypeError: int() argument must be a string, a bytes-like object or a number, not ‘Eval’

A Bug ?


(Cédric Krier) #6

Indeed dynamic domain is not allowed for filter because it is used in the Field.get which can not be evaluated.
We should probably put a warning in the documentation about this restriction.


#7

Ok, so no solution for my initial question, I’m looking for another way to do it.
Thank you.


(Cédric Krier) #8

I think you should explain why you need that because custom domain may not be the solution.


#9

well, I would like to display in the selection view of the Many2Many only the records of the current company. However it is possible that in the table I have records for another company.
Thanks for any suggestion to make that.


(Cédric Krier) #10

I do not understand how it is possible to have record from another company if it is not possible to select them. So for me, it is a classic domain.


#11

I have an object with a company field that I use in my Many2Many. I would like that when I’m on company 1 I can add to my Many2Many only the records from company 1 and that when I’m on company 2, only the records of company 2.
If I’m not mistaken, it looks like the result obtained with ‘CompanyValueMixin’ but on a Many2Many


(Sergio Morillo) #12

It sounds like a simple company domain:

[('company', '=', Eval('company'))]


#13

Not completely, because, like i writed in my first post, when you have in the many2many a record with company 1 you have a domain violation when you try to write a record for company 2.


(Sergi Almacellas Abellana) #14

In a Many2Many relation there are two fields to be defined:

  1. The Many2Many fields on the record model which have the company relation.
  2. The Many2One on the intermediary table that will store the data.

It seems you are defining the domain on the field 2 which will be applied on all records (and that’s why you have the domain violation for all companies) but it should be defined on the field 1.

Hope it helps!


(Cédric Krier) #15

You can probably use record rule to allow only to ‘read’ target from the current company. But this will apply everywhere.
Or you must use a function field that perform the filtering for you (a little bit like the moves on shipments).


#16

Thank you for your answer, that’s what I did.


(system) #17

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.