4

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       
KbiR
  • 4,047
  • 6
  • 37
  • 103
Borealis
  • 1,059
  • 2
  • 18
  • 37

3 Answers3

4

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
KbiR
  • 4,047
  • 6
  • 37
  • 103
Charif DZ
  • 14,415
  • 3
  • 21
  • 40
3

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

KbiR
  • 4,047
  • 6
  • 37
  • 103
Ropo
  • 144
  • 1
  • 13
  • @SOS-Mona This is the correct answer. Selection fields require a `selection` parameter that defines what the choices are for the field. It defaults to `None`, so you would be unable to set any meaningful value. Take a look at the [documentation for Selection fields](http://www.odoo.com/documentation/9.0/reference/orm.html#openerp.fields.Selection). "The attribute selection is mandatory except in the case of related fields or field extensions." – travisw Aug 20 '17 at 05:09
1

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)
qvpham
  • 1,896
  • 9
  • 17