Ir al contenido
Menú
Se marcó esta pregunta
4 Respuestas
1752 Vistas

I have these buttons:




def update(self):

evaluation_cycle_model = self.env[
'sst.initial.evaluation.by.cycles'].search([('id', '=',self.evaluation_id.id)],limit=1)

if evaluation_cycle_model:
evaluation_cycle_model.update_records()

view_id = self.env.ref('sst.view_sst_qualification_form').id
return {
'type': 'ir.actions.act_window',
'name': 'Calificar Items',
'res_model': 'sst.initial.evaluation.by.cycles',
'res_id': self.id,
'view_mode': 'form',
'views': [[view_id, 'form']],
'target': 'new',
'tag': 'reload'
}

def action_discard(self):

view_id = self.env.ref('sst.view_sst_qualification_form').id
return {
'type': 'ir.actions.act_window',
'name': 'Calificar Items',
'res_model': 'sst.initial.evaluation.by.cycles',
'res_id': self.id,
'view_mode': 'form',
'views': [[view_id, 'form']],
'target': 'new',
}


I have this action_discard button, which returns an action to return to a specific view, my question is, how can I make the button works? The action it returns works, but it does not discard the information, on the contrary it saves it in the database.

Avatar
Descartar
Mejor respuesta

To implement a custom "Discard" button in Odoo 17 that effectively reverts unsaved changes, you can follow these steps:

  1. Define the Discard Button in Your XML View:
    Add a button to your form view with the special="cancel" attribute. This attribute ensures that the button functions as a discard action.
    xml Copy code < footer > 
        < button  string = "Discard"  special = "cancel"  class = "oe_link" /> 
    </ footer >
    
    The special="cancel" attribute is recognized by Odoo to trigger the discard operation, reverting any unsaved changes and closing the form view.
  2. Implement the Discard Functionality in Your Python Model:
    If you need to perform additional actions when the discard button is pressed, you can define a method in your model. However, for the standard discard behavior, the special="cancel" attribute handles the necessary operations.
    python Copy code from odoo import models, fields, api
    
    class  YourModel (models.Model):
        _name = 'your.model'
    
        # Define your fields here
    
        @api.multi 
        def  action_discard ( self ):
             # Custom actions before discarding changes 
            self.env.cr.rollback()   # Revert unsaved changes 
            return {
                 'type' : 'ir.actions.act_window' ,
                 'name' : 'Your Form ' ,
                 'res_model' : 'your.model' ,
                 'view_mode' : 'form' ,
                 'res_id' : self. id ,
                 'target' : 'current' ,
            }
    
    In this method, self.env.cr.rollback() is used to discard any uncommitted changes in the current transaction. The returned action reloads the form view without saving the modifications.

Additional Considerations:

  • Using special="cancel" : This attribute is a built-in feature in Odoo that provides standard discard functionality without the need for custom methods.
  • Custom Discard Logic: If your discard operation requires additional logic, such as resetting specific fields or triggering other actions, you can define a method like action_discard in your model and link it to a button without the special="cancel" attribute.

By following these steps, you can create a custom discard button in Odoo 17 that effectively reverts unsaved changes, ensuring a seamless user experience.

Sources

Avatar
Descartar
Mejor respuesta
  • self.env.cr.rollback() will discard any changes made in the current transaction.
  • This prevents the record from being saved when returning to the form view.

Now, when the action_discard button is clicked, the record’s changes will be discarded and the user will be taken back to the form view without saving any modifications.

Avatar
Descartar

Hope this answer helps you. If you have any questions, you can contact me via the following page: https://basketballlegends.pro
Playing in the basketball competition is a great way to get in on the championship race and join the other competitors.

Mejor respuesta

Hi! I like yours very much. I think you should check out for some custom dismiss button options. It helps you a lot in this regard.

Avatar
Descartar

The best idea is to refer to https://geometrydashdeadlocked.com/ to have suitable selection methods for the above problem.

Autor Mejor respuesta

I did it guys.
I just save each field in the context:

'context': {
'default_item_id': self.id,
'default_evaluation_id': evaluation_id,
'default_item_weight': self.weight,
'default_date': evaluation.date,
'default_compliance': evaluation.compliance,
'default_weight': evaluation.compliance_weight,
'default_responsible': evaluation.responsible.id,
'default_observation': evaluation.observation
}
def action_discard(self):
original_data = {
'date': self.env.context.get('default_date'),
'compliance': self.env.context.get('default_compliance'),
'compliance_weight': self.env.context.get('default_weight'),
'responsible': self.env.context.get('default_responsible'),
'observation': self.env.context.get('default_observation')
}
self.write(original_data)
view_id = self.env.ref('sst.view_sst_qualification_form').id
return {
'type': 'ir.actions.act_window',
'name': 'Calificar Items',
'res_model': 'sst.initial.evaluation.by.cycles',
'res_id': self.id,
'view_mode': 'form',
'views': [[view_id, 'form']],
'target': 'new',
}
Avatar
Descartar
Publicaciones relacionadas Respuestas Vistas Actividad
3
mar 25
1962
0
mar 25
813
1
nov 24
1821
2
abr 25
2056
0
feb 25
696