Skip to Content
Menu
This question has been flagged
3 Replies
5894 Views

Hi all,

I'm inheriting hr in my custom model and intend to extend toggle_active() which is is defined in root model.

@api.model
def toggle_active(self): 
_logger.info('Executing toggle_active'
 record=super(hr_employe_custom_model,self).toggle_active()
return record


The function is not called when toggling the active state via the button. The obvious workaround with

@api.onchange('active')def _onchange_active(self):


is not executed either. What do I miss here?

Thanks & Regards, Matthias

Avatar
Discard
Best Answer

Hi,

have a look at the example in the res_users file:

    @api.multi
    def toggle_active(self):
        for user in self:
            if not user.active and not user.partner_id.active:
                user.partner_id.toggle_active()
        super(Users, self).toggle_active()
                  If it doesn't work for you, check:
                  * That the python file is initiated. Are you sure you add import to the init file?
                  * Check that this method is not re-written in 2 independant modules.

                  Besides, if you want to add a certain write when 'active' is toggled, it seems more reasonable to use 'inverse'. The reason is that active migth be changed not only through the button. An example:

                  @api.multi
                  def _inverse_active(self):       
                      for record in self:               
                          pass # do some stuff with each changed record
                  active = fields.Boolean(inverse=_inverse_active)
                  Avatar
                  Discard
                  Best Answer

                  The @api.model here is out of place, since you are making changes to a record not the model itself. Try it with @api.multi instead
                  Also, you are returning "record", while you never assigned any value to it. It seems weird that this didn't raise any error

                  Avatar
                  Discard
                  Author Best Answer

                  OK, my PyCharm Env crashed earlier, but my python process was still alive, thats why my changes in code weren't executed as the old process answered my requests... Btw, why isn't there any check if there is already running an instance for the given port?

                  Anyways,

                  def toggle_active(self):
                      super(hr_employe_custom_model,self).toggle_active()
                  works as intended! Thanks a ton!
                  Avatar
                  Discard