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

Hello All,

I have a char type field and for this field i want to add constraints for make this field unique. Means no record with same name should be enter by user. So, how to use  sql constaints for this field uniqueness in Odoo10.

Thanks in advance.


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

I already added this, but it's not work for me. Your solution from old api link, i already seen this link

_sql_constraints = [

('rental_manag_location_uniq', 'unique(location_id)', 'Location ID must be unique !'),

]

Ảnh đại diện
Huỷ bỏ

Try it in new database, you can get it,

Câu trả lời hay nhất

Hai Pawan Sharma, 

_sql_constraints = [        ('name_uniq', 'UNIQUE (name)',  'You can not have two users with the same name !')    ]

'name_uniq' is constraint name give any name.(no need to define)

Example _constraints:-

    def _check_name(self, cr, uid, ids, context=None):
        for val in self.read(cr, uid, ids, ['name'], context=context):
            if val['name']:
                if len(val['name']) < 6: 
                   return False       

            return True

    _constraints = [        (_check_name, 'Name must have at least 6 characters.', ['name'])    ]

Note: Based on the above example in the records name field any duplicate record or name field less than 6 character then remove the records and add the constraints in python restart the server, update the module then only it works.

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất
 _sql_constraints = [
('value_campus_studenttype_policyfor_uniq', 'unique (campus_id,student_type,policy_for)', 'Record for this Campus and Student Type and Policy are already exists !')
]

Try this one

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
2
thg 7 25
484
0
thg 12 24
1092
2
thg 10 24
1196
1
thg 8 24
2355
2
thg 7 24
1592