跳至内容
菜单
此问题已终结
1 回复
4769 查看

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.

相关帖文 回复 查看 活动
3
5月 24
10841
1
11月 24
1721
1
9月 21
9523
1
1月 24
17093
0
3月 15
3372