Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
5 Trả lời
28148 Lượt xem
How to Disable edit form fields when state is done of form.

state = fields.Selection([
('draft', "Draft"),
('confirmed', "Confirmed"),
('done', "Done"),
], default='draft')

@api.multi
def action_draft(self):
self.state = 'draft'

@api.multi
def action_confirm(self):
self.state = 'confirmed'

@api.multi
def action_done(self):
self.state = 'done'
Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

You can add the record rule to restrict the write access when the state is Done. 

In the record rule, You add the domain_force for state is done. Then You give the permission to read only.


 <record model="ir.rule" id="no_edit_in_done_state">

           <field name="name">No Edit in done state</field>

            <field name="model_id" ref="model_your_model_name"/>

           <field name="groups" eval="[(4, ref('base.group_user'))]"/>

           <field eval="0" name="perm_unlink"/>

           <field eval="0" name="perm_write"/>

           <field eval="1" name="perm_read"/>

           <field eval="0" name="perm_create"/>

           <field name="domain_force">[('state', '=', 'done'])]</field>

   </record>

If You using this record rule, the all fields are readonly when the state is done.   

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

The best solution,

if you need to edit that form later, you can add a button in which you reset the state with .sudo().

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

Put attrs on every fields which you don't want to let edit in edit mode after state = done.


<field name="any_field_on_form" attrs="{'readonly':[('state','=','done']}" />

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

record rule best solution but doesn't work

any help ?


 <record model="ir.rule" id="no_edit_in_done_state">

<field name="name">No Edit Or Delete in Awaiting Approval state</field>
<field name="model_id" ref="model_bid_bids_screen"/>
<field name="groups" eval="[(4, ref('bid.group_workers'))]"/>
<field eval="0" name="perm_unlink"/>
<field eval="0" name="perm_write"/>
<field eval="1" name="perm_read"/>
<field eval="0" name="perm_create"/>
<field name="domain_force">[('state','=','awaiting_approval')]</field>

</record>
Ảnh đại diện
Huỷ bỏ

Have you tried adding your module name in ref?

<field name="model_id" ref="your_module_name.model_bid_bids_screen"/>

I think I'm wrong, never mind my comment.

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

You can also do this in the python code itself:

field_name = fields.field_type(string='Field name', store=True, readonly=False, states={'done': [('readonly', True)]})


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