Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
3 ตอบกลับ
46653 มุมมอง

When I create a custom model in the web GUI, how can I mark a field as "unique", so that the database will never create a second row with the same field value?

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

You can only do this when you create your field via Python.

See https://accounts.openerp.com/forum/Help-1/question/16386

_sql_constraints = [
                     ('field_unique', 
                      'unique(field)',
                      'Choose another value - it has to be unique!')
]

Note that if there are existing records for the model in the database, then any field values have to be unique, or this will fail and OpenERP will do nothing to enforce the constraint for new records.

อวตาร
ละทิ้ง
ผู้เขียน

Thanks, so I will have to write a Python module for my table. Maybe it would be a nice enhancement if there would be a "unique" flag in the GUI as well. I assume, that unique fields are needed often (serial numbers, order numbers, etc.).

คำตอบที่ดีที่สุด

Hi,

add a sql constraint to your model :

_sql_constraints = [
        ('uniq_name', 'unique(stuff_name)', "A stuff already exists with this name . Stuff's name must be unique!"),
    ]

Best regards

อวตาร
ละทิ้ง
ผู้เขียน

Thanks, too!

คำตอบที่ดีที่สุด
_sql_constraints = [
        ('uniq_name', 'unique(your_field_name)', "You Entered data is already exists with this name. Please give data must be unique!"),
    ]
อวตาร
ละทิ้ง
Related Posts ตอบกลับ มุมมอง กิจกรรม
1
มี.ค. 15
7007
2
มี.ค. 15
6291
2
มี.ค. 22
2538
0
มี.ค. 24
3189
3
มิ.ย. 17
7001