Hilfe

0

Remove duplicates on selection many2one, one2many

Avatar
Fatih Piristine

tried many different ways yet couldn't find any solution so far. checked all source code hoping to find an example yet nothing.

here is what I am trying to do:

food and ingredients ( aka nutrient ). ingredients must be selected once and not shown twice in drop-down.


i can control with domain to filter for first selection. no problem.

when adding another nutrient. (in the background) 'Vitamin A' is already on the list and that should be left out.

any idea how to achieve it ? keep in mind that record is still on create mode. nothing in db yet.

thanks,


note: many2one and one2many widgets are able to do this filtering but not drop-downs



1 Kommentar
Avatar
Verwerfen
Avatar
Fatih Piristine
-

Nikhil, thanks for the hint. that helped a lot. solved the problem.

can't convert your comment as answer, not having enough karma points.

2 Antworten
1
Avatar
Nikhil Krishnan
Best Answer

I think Using python we can set a manual domain for the Nutrient field and achieve your requirement.

return like this

return {

'domain': {'field': [('********_ids', '!=', selected_ids)]},

}

selected ids first find by search and try.

4 Kommentare
Avatar
Verwerfen
Avatar
Fatih Piristine
-

Nikhil, this one works when using save&close button without any problem. but save&new button is totally problem. do you have any idea? thank you.

Avatar
Nikhil Krishnan
-

When you click save & new it runs the onchange function, I think that function makes the issue, you just post the issue and code too, then only we can find or suggest the issue.

Avatar
Fatih Piristine
-

actually it doesn't trigger onchange function for group/nutrient. it triggers the onchange function for values (many2one). i altered the context to pass selections in between but in current mode, contexts are different for values and group/nutrient on onchage functions.

Avatar
Fatih Piristine
-

well, things got complicated so hid that button with css for now. i ll look into it later.

0
Avatar
Fatih Piristine
Best Answer

final result:

<page string="Ingredients">
<field name="values" context="{'values': values}">
...



def onchange_group_id(self, cr, uid, ids, group_id, context=None):
if not group_id:
return {'value': {'group_id': False, 'nutrient_id': False}}

# how to improve here ??

exclude = []
if context and 'values' in context:
for ntr in context['values']:
if len(ntr) >= 2:
exclude.append(ntr[1])

result = {'value': {'group_id': group_id, 'nutrient_id': False}}

if len(exclude) > 0:
# id -> nutrient_id.
result.update({'domain': {'nutrient_id': [('id', 'not in', exclude)]}})

return result
Avatar
Verwerfen