Skip to Content
Menu
This question has been flagged
2 Replies
8775 Views

If the user already selected a value (list generated from Many2one) in a record, Now I dont want to show that value in the list that is generated from Man2one fields for any new form (records) to avoid same many2one value in two different records

Avatar
Discard
Best Answer

Hi,

Try to return domain by id not in [list of ids ie, already used in prev records], either you can return domain in onchange.

Avatar
Discard
Author

Can you please post any example, I have no clue how to do this.

@api.onchange('test_domain')

def onchange_test_domain_fiedl(self):

#here use your model to fetch the records

obj = self.search([])

available_ids = []

for i in obj:

# appends the man2one fields idsss

available_ids.append(i.test_domain.id)

return {'domain': {'test_domain': [('id', 'not in', available_ids)]}}

test_domain = fields.Many2one('sale.order', "Testttttttt")

Dont forget to upvote if my answer helps you

Author

Hi Hilar,

Thanks for your Precious time, This is exactly what i wanted.

Thank you sir!

Best Answer

You can restrict selection on field that already selected using _sql_constraints.

_sql_constraints = [ ('many2one_field', 'unique (many2one_field)','This one is already selected.') ]
Avatar
Discard
Author

Thank for the response, but It is not good in term of User Experience, It will be best if we hide that value before showing error to user

Related Posts Replies Views Activity
1
Nov 22
14647
3
Aug 22
10754
2
Aug 22
3045
0
Jul 22
835
2
Oct 21
1435