Skip to Content
Meniu
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
Această întrebare a fost marcată
5 Răspunsuri
29264 Vizualizări

Hi !

I wrote an @api.onchange method (ation_afaire) that detects a change in a Datetime field and calls another method (action_confirm) of the same class which among other things changes the order status to 'sale'. Everything works fine, when I make a change in the date_realization field the order state changes to sale. The problem is that when I click on save the system doesn't pick up the change and it all goes back to the previous state !

Does anybody knows why I am being unable to save this state change ?

Thank you !

Here's the code:

# -*- coding: utf-8 -*-

from odoo import models, fields, api

class gescom_states(models.Model):

    _inherit = 'sale.order'

    date_realization= fields.Datetime(string='Date de réalisation', store=True)

@api.multi
    def action_confirm(self):
        for order in self:         
            order.state = 'sale'          
        if self.env.context.get('send_email'):
                self.force_quotation_send()
            order.order_line._action_procurement_create()
        if self.env['ir.values'].get_default('sale.config.settings', 'auto_done_setting'):
            self.action_done()          
        return True

    @api.onchange('date_realization')
    def action_afaire(self):
        super (gescom_states,self).action_confirm()

Imagine profil
Abandonează

do you want to call the function action_confirm while onchange of the field 'date_realization'?

do you have all fields (that you want to be saved) on the view XML ? If not needed / visible, you have to put them invisible, and so they'll get saved

Cel mai bun răspuns

That happens because 'state' field in sale.order model is readonly (readonly=True). 

In order to force it to be written you need to use write() function.

Imagine profil
Abandonează

That's correct

I tried that and it didn't work, what I did was in the view add an attribute force_save='1'

Cel mai bun răspuns

onchange methods work on virtual records - assignment on these records is not written to the database, just used to know which value to send back to the client.

From https://www.odoo.com/documentation/12.0/reference/orm.html

Imagine profil
Abandonează
Cel mai bun răspuns

Hello,
As Ray Carnes mentioned that onchange methods work on virtual records.
So in onchange() method doesn't contain original record in which you changing. in this method "self" contains virtual copy of the record.

so, in your case it will not confirm that record with action_confirm().

Imagine profil
Abandonează
Cel mai bun răspuns

In a @api.onchange function, you have to use write() function of the model


@api.onchange('field_name')
def _function_name(self, value)
    _my_object = self.env['model.name']
    _myobject.write({'field_name' : value})

If you want to edit a model refered by a Many2one, you you directly use this field as

@api.onchange('field_name')
def _function_name(self, value)
    self._my_many_2_one_field.write({'field_name' : value})
Imagine profil
Abandonează
Related Posts Răspunsuri Vizualizări Activitate
1
mai 25
936
2
nov. 24
2164
1
apr. 24
2800
4
feb. 24
12295
1
ian. 24
1660