This question has been flagged
3 Replies
6706 Views

Hi everyone,

For what I know, there's no action called when we click on Create or Edit buttons on Odoo.

The actions are called when we click on "Save" for both situations.

If we are creating a new record, the action called during save is "create".

If we are modifying a record, the action called is "write".

Is there any way I can predict if a record is being created or edited?

Thank you in advance

Best regards

Paulo

Avatar
Discard
Best Answer

Hi Paulo,

You can create a Boolean  field called 'is_saved' in your model and give it a default value of False. Then overwrite the create method to update the value to True after the record has been successfully saved. Here is the code;

class SomeModel(models.Model):
...

is_saved = fields.BooleanField(default=False)

@api.model
def create(self, vals, *args, **kwargs):
record = super(SomeModel, self).create(vals, *args, **kwargs)
record.is_saved = True
return record

Then you can tell whether you are editing or creating a record by calling this field. For example on your onchange method you can do:

@api.onchange('some_field')
def onchange_some_field(self):
vals = {}
if not self.is_saved:
vals.update({...})
return {'value': vals}
Avatar
Discard
Best Answer

if you have specific action and you want to run for both cases,

you can override the create and write method and specify the action inside them,

but generally create functions runs only once and the each save operation will call the write function



Regards

Avatar
Discard
Best Answer

Hi Paulo,

I am not perfectly understanding you, what actually you want to do.

If you want do detect that record is creating or editing then you can override create() and write() accordingly. and in that you can get created new values and edited new values in create()/write()

See reference: https://goo.gl/FM1B72


if you want to detect event then you have to do JS code i think..


accept and upvote answer if helpful.

Thanks and regards

Haresh Kansara

Avatar
Discard
Author

Thank you @Haresh,

What I want to do is to "detect" the - event itself. Not create/write at the time we save the form.

Any ideias on how I can detect this events with JS?

Thank you once again