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

At first I thought this would be easy, since it's not a big deal. I'll go straight to the problem.

def verify_service(self, cr, uid, ids, context=None):
    record = self.browse(cr, uid, ids)[0]
    rec_value = record.nr_service
    cr.execute("SELECT nr_service FROM services WHERE nr_service = %s", (rec_value,))
    values = cr.fetchall()
    if not values: # It means the database has NO value
        return True # continues to save the Form
    else: 
        return False # If there's any value with the value of the field "rec_value" it returns error!

_columns = {
        'nr_service':fields.integer('Nr. Service', size = 30, required = True),

_constraints = [
                    (verify_service, 'Error: Already that ID in the database!', ['nr_service']),

Isn't this so simple? But no matter what value I put inside the 'nr_service' field, it displays always the constraint message. What am I doing wrong?

อวตาร
ละทิ้ง
ผู้เขียน คำตอบที่ดีที่สุด

Solved.

def verify_service(self, cr, uid, ids, context=None):
    record = self.browse(cr, uid, ids)[0]
    rec_value = str(record.nr_service)
    cr.execute("SELECT COUNT(*) FROM services WHERE nr_service = %s", [rec_value])
    values = cr.fetchone()
    if values[0] <= 1:
        return True
    return False
อวตาร
ละทิ้ง
Related Posts ตอบกลับ มุมมอง กิจกรรม
0
มี.ค. 15
5033
@api.constrains แก้ไขแล้ว
2
ธ.ค. 23
3528
0
ก.ย. 23
6314
0
มิ.ย. 21
2792
1
พ.ย. 20
4573