Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the value of a selection field on create function?

Tags:

odoo

odoo-10

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       
like image 873
Borealis Avatar asked Jan 29 '23 18:01

Borealis


2 Answers

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
like image 126
Charif DZ Avatar answered Mar 28 '23 05:03

Charif DZ


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

like image 22
Ropo Avatar answered Mar 28 '23 06:03

Ropo