How to Unique constraint a field in custom model on Odoo Enterprise 12.0 Online?
Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:
- CRM
- e-Commerce
- Księgowość
- Zapasy
- PoS
- Project
- MRP
To pytanie dostało ostrzeżenie
Hi Tameem,
I think you are trying to make the Unique field.
Usually, this is handled by SQL Constraints. In your case, you are using Odoo Online. So we must think differently to make it possible.
Anyway
Try this way. Maybe you will get the result you wish.
Create an Automated action with the following parameters.
Model: your custom model
Trigger Condition: On Update
Action To Do: Execute Python Code
In the python code, try this code
new_value = record.YourFieldName
existing_records = env['Your.Model'].search([('YourFieldName', '=', new_value)])
if len(existing_records) > 1:
raise Warning('Field value must be unique')
Thanks
Dear Avinash,
Your answer worked perfectly!
Thank you for your reply.
My pleasure
Hello, do you know if we can manage to do the same using Studio ?
Hello, Avinash's answer works for me too but instead of having the warning I setted I have an Odoo error about the automated action itself. Do you know how to solve this ?
Podoba Ci się ta dyskusja? Dołącz do niej!
Stwórz konto dzisiaj, aby cieszyć się ekskluzywnymi funkcjami i wchodzić w interakcje z naszą wspaniałą społecznością!
Zarejestruj sięPowiązane posty | Odpowiedzi | Widoki | Czynność | |
---|---|---|---|---|
|
1
lis 24
|
1391 | ||
|
2
sty 24
|
4701 | ||
|
3
lip 25
|
548 | ||
|
2
lip 25
|
3858 | ||
How to make email on res.partner be unique
Rozwiązane
|
|
2
wrz 21
|
4857 |
See also this similar question with some useful answers: https://www.odoo.com/forum/help-1/question/how-can-i-prevent-users-from-entering-duplicate-vendors-based-on-name-only-134963#answer-134964