Skip to Content
Menu
This question has been flagged
4 Replies
9892 Views

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



Avatar
Discard
Author

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

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

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.

Avatar
Discard
Author

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.

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.

Author

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.

Author

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

Author 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
Discard
Related Posts Replies Views Activity
1
Mar 23
787
0
Dec 22
1515
0
Jun 21
1843
0
Jun 20
4179
1
Nov 19
1743