Skip to Content
Menu
This question has been flagged
2 Replies
11696 Views

Hi,

  I need to make all the fields in the entire form read-only when   the state is done.I know we can do it by adding attrs="{'readonly':[('state','=','done']}" for each field. But i am looking for another method.i tried by adding record rule.But it doesn't work at all.

Avatar
Discard
Best Answer

If the question was about edit-ability - and ir.rule does not work for you (as it did not work for me, trying and searching for hours), then you can extend the write method:

    @api.multi
    def write(self, vals):
        if any(state == 'done' for state in set(self.mapped('state'))):
            raise UserError(_("No edit in done state"))
        else:
            return super().write(vals)

Trying to edit in done state will result in the Alert with your User Error message.

Avatar
Discard

I used your suggestion Michael, and it worked fabulously. I went alittle further with it to check against called methods, something like this:

@api.multi

def write(self, vals):

invoice_refund = request.params.get('method') in 'invoice_refund'

move_reconcile = request.params.get('method') in ('assign_outstanding_credit', 'remove_move_reconcile')

group_account_edit = self.env.user.has_group('custom_sec_group.group_account_edit')

_logger.debug('Requested params method: [%s.%s]' % (request.params.get('model'), request.params.get('method')))

_logger.debug('Allow invoice_refund method: %s', invoice_refund)

if any(state != 'draft' for state in set(self.mapped('state'))

if not (group_account_edit or invoice_refund or move_reconcile)):

raise UserError(_('Edit allowed only in draft state. [%s.%s]' % (request.params.get('model'), request.params.get('method'))))

else:

_logger.info('Written vals: %s', vals)

return super().write(vals)

Hi, where should I put this write method?

@cteng You'll need to extend the model for which you want to disallow editing and override the write there.

Best Answer

@Michale Jurke, I tried your solution but got:
`AttributeError: module 'odoo.api' has no attribute 'multi'` 

Avatar
Discard

remove @api.multi

Related Posts Replies Views Activity
2
Oct 18
2051
3
Oct 18
2769
1
Mar 18
3435
0
Nov 17
1849
2
Apr 23
10043