4
votes

I wanna change the value of status once I click save. status is a selection field [('ok', 'Ok'),('tobe', 'Not Ok')]

    status = fields.Selection(
        readonly=False,
        default='tobe',
        related= 'name.status'      
    )
    @api.model
    def create(self, values):
        self.status= 'ok'
        line = super(MyClass, self).create(values)
        return line       
3

3 Answers

4
votes

Status is a related field so after creating change the status of you many2one field.

  @api.model
  def create(self, values):
         rec = super(YouClassName, self).create(values)
         # here change the status. 
         rec.name.status = 'ok'
         return rec
3
votes

The error is in your selection field declaration. It should be like this:

status = fields.Selection([('ok', "OK"),('not ok', "Not OK"),],default='tobe')

@api.multi
def write(self, vals):
    vals['status'] = 'ok'
    ret = super(Your-Class-Name-here, self).write(vals)

By default, readonly on every field is false, so no need specifying it inside the selection field.

Please, notify me if this solve your challenge. Thanks

1
votes

On the time, when the method create is called, your instance isn't created. So self doesn't have any instance. self.status = 'ok' will change the value of status of nothing.

You can set the value in values like that:

@api.model
def create(self, values):
    values['status'] = 'ok'
    line = super(MyClass, self).create(values)
    return line

Or change the value after create instance:

@api.model
def create(self, values):
    line = super(MyClass, self).create(values)
    line.status = 'ok'
    return line

But the method create is called, only when a new instance is created. There is the case, that someone want to save the instance. Then you must override the method write:

@api.multi
def write(self, vals):
    vals['status'] = 'ok'
    ret = super(FrameworkAgreement, self).write(vals)