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

I need to have a form where some fields are editable when the user creates the object, but not editable when the user is editing it. 

I mean:

  • When the user clicks create, the field must be editable.
  • When the user access an existing object (using a list) and clicks on edit, the field must be readonly.

 

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

A record that is not already saved once, has no id. So you can relly on that:

readonly: [('id','>',0)]

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

Thanks, that's a good idea. Unfortunately it's always readonly, for creating and editing.

ผู้เขียน

I have: 'cus_id': fields.related('quot_id', 'pr_id', 'cus_id', type="many2one", relation="bdd.customers", string="Customer", store=False, readonly="[('id','>',0)]"),

ผู้เขียน

I think that the problem is that readonly is only evaluated when the object is created.

Then you can set a value in 

_defaults = {
  'new': True
}

that makes it possible to distinguish between a new record and a already created. When you overwrite the write(), you can reset the value:

def write(self, cr, uid, ids, vals, context=None):
   vals['new'] = False
   return super(res_partner_ext, self).write(cr, uid, ids, vals, context=context)

ผู้เขียน

It worked. Actually I needed to implement it in create instead of write, but it works. The only that I don't like is to add a new field in the table, but it works. Thanks.

Related Posts ตอบกลับ มุมมอง กิจกรรม
3
พ.ค. 24
10867
1
พ.ย. 24
1729
1
ก.ย. 21
9536
1
ม.ค. 24
17111
0
มี.ค. 15
3397