Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
3 Trả lời
764 Lượt xem

Hello Odooers, 

Using Odoo Studio, I'm trying to find how I can make a unique field with an error message (ex. This Order ID already exists). 

I see some posts with the code below, but I have no idea where to insert it ? and if that's only a python code that cannot be used in Studio ?


_sql_constraints = [

        ('name_uniq', 'unique (name)', "Tag already exists!"),

    ]


Thanks for the help !

Much appreciate.

Ảnh đại diện
Huỷ bỏ
Tác giả Câu trả lời hay nhất

Thank you both. I will test it right away.

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hi,

You can add SQL constraints using a server action.


Example:

Steps:

1. Settings- > Technical -> Actions -> Server Action




Model: Sales Order

Type: Execute Code

Code:

env.cr.execute("""

    ALTER TABLE sale_order

    ADD CONSTRAINT unique_name UNIQUE (name)

""")


- unique_name = name of the constraint

- name = field


Hope it helps.



Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

The answer you've commented on in https://www.odoo.com/forum/1/155133 already says what to do if you can't do it with a module - i.e. Odoo Online. You need to create an Automation Rule that is triggered On Update and On Create to check if other records with that value already exist. You can not use the Studio UI directly if this is what you're asking explicitly.

_sql_constraints = [] is python code you can not add directly.

Automation Rules are created, when in Developer Mode (https://www.odoo.com/documentation/18.0/applications/general/developer_mode.html), via Settings -> Technical --> Automation -> Automation Rules.

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 10 22
3363
2
thg 9 21
3791
2
thg 8 25
3608
2
thg 2 25
6645
3
thg 9 24
47117