Override create functions in Odoo

In this tutorial I will teach you how to override the default create function from Odoo. You will learn how to override the create function and how to change values on a record before storing it in the database.
In this tutorial I will use the ‘res.partner’ model, which is the model from where you can create and edit contacts. I will override this create function, print a message in the terminal and will change the value of a field before storing the record in the database.

1. Inheriting the model

The first thing that you need to do is create a new python file in the ‘models’ folder. Open the ‘models’ folder of your custom module and create a new Python file named ‘res_partner.py’. After creating this file don’t forget to import it in ‘__init__.py’.
Now, open up the ‘res_partner.py’ file, import Odoo, add a class name and inherit the ‘res.partner’ model. Your code should now look like this:

2. Override the create function

Now that we have access to all Odoo methods (thanks to the imports) and now that Odoo knows which model we want to inherit we can start inheriting the create function.
The create function is a default function from Odoo so we can use the API that is provided by Odoo, this is where we use @api.model. After calling @api.model we need to define a function name and pass self and values to the function:

At this point Odoo knows we are doing these operations on the model ‘res.partner’ and that we created a new function with the decorator @api.model.
Tip: Want more information about the Odoo API and the model decorator? See the official documentation
At this point Odoo doesn’t know that we want to inherit an already existing function though. This is where the super call comes in action. With super we can call the original function and override it to change the default behaviour. If you’d like more information about using super see the official Python documentation.
Now, call the super method, pass your class along and tell Python which function you want to override:

So, what exactly does this code do? It tells Python to call the original create function for the model ‘res.partner’ and to apply all the current values to the variable ‘record’. ┬áthe variable ‘values’ contains all the values and is passed in the .create() function so that all variables are in ‘record’. In the variable ‘record’ will be all values that you’ve filled in on the form up untill the point you clicked save.

2.1 Changing values in the super

Now, let us go a step further, and add a new field to the model ‘res.partner’ to change a variable the moment we override this function.
Add the following field to your model:

Usually when you override a default function you want to change the value of a field or apply a value. In this example I will set the just created field ‘passed_override_write_function’ to true so that this value is also stored in the database. You can get, or change, any value on this record by calling record[‘field_name’] to change it. Set the variable ‘passed_override_write_function’ to true every time we pass this function:

2.2 Returning the super call

The final step is to return the record and stop the function. Without this your function will not work and the new values will not be returned. Returning the record is very simple:

Let us have a look at the full code. Your ‘res_partner.py’ file should now look like this:

Congratulations, that is all! You’ve now overriden the create method, changed a value in it and stored it in the database. Now create a new contact and click on ‘Save’. The result in the terminal:New contact creation

The result in the database:Database result after override

3. Conclusion

Overriding existing functions in Odoo is quite easy to do. Odoo is built on Python and Python allows you to use super calls, which makes it quite easy to override existing functions.

Do you want to try the demo code and see the source code of this tutorial? You can view it on my Github account.
Has this tutorial helped you, do you have any feedback or questions? Post away!




PayPal

Odoo Experts