Odoo tutorials - Odoo made easy [PDF]

In this tutorial I will teach you how to automatically configure app settings with erppeek in Odoo 11. You will learn ho

42 downloads 44 Views 3MB Size

Recommend Stories


Formation Technique Odoo
Just as there is no loss of basic energy in the universe, so no thought or action is without its effects,

[PDF] Review Odoo Development Cookbook Get Online
It always seems impossible until it is done. Nelson Mandela

Scarica Gratis Odoo 10 Development Essentials PDF
We can't help everyone, but everyone can help someone. Ronald Reagan

a cura dell'Associazione Odoo Italia
The only limits you see are the ones you impose on yourself. Dr. Wayne Dyer

Download Financial Accounting with Odoo, Second Edition
Be like the sun for grace and mercy. Be like the night to cover others' faults. Be like running water

[PDF]} Hibernate Made Easy
You're not going to master the rest of your life in one day. Just relax. Master the day. Than just keep

Read Financial Accounting with Odoo, Second Edition
Raise your words, not voice. It is rain that grows flowers, not thunder. Rumi

Download PDF Calculus Made Easy
The beauty of a living thing is not the atoms that go into it, but the way those atoms are put together.

PDF Download Ayurveda Made Easy
How wonderful it is that nobody need wait a single moment before starting to improve the world. Anne

[PDF] Pharmacology Made Incredibly Easy
You have survived, EVERY SINGLE bad day so far. Anonymous

Idea Transcript


Configuring app settings with erppeek in Odoo 11 NOV E MB E R 18, 2017

Odoo tutorials Odoo made easy

Search …

Y E NTHE 666

LE A V E A COMME NT

In this tutorial I will teach you how to automatically configure app settings with erppeek in Odoo 11. You will learn how to use the erppeek library and how to configure app settings from Python code. By doing so you can automate the configuration of your database(s). In this tutorial I will configure the sales app to use different units of measure through Python code. Tip: This tutorial will only work for Odoo 11. The concept will work for Odoo 8, 9 and 10 too but the code will be slightly different.

1. Installing erppeek RECENT POSTS Configuring app settings with erppeek

Before we can start using erppeek we will need to install it. Open up a Linux terminal and run the following command:

in Odoo 11 Installing Odoo 11 (enterprise) on

After running this command we can access and use erppeek.

Ubuntu

2. Creating the Python script Installing and configuring Odoo on

Now create a new Python file on your system where you would like to save your new script:

DigitalOcean Extend existing selection with selection_add in Odoo 10 Creating webpages and controllers in Odoo 10

2.1 CONFIGURING THE SETTINGS Now that we have a new file we have to import erppeek, so we can access all the possibilities of the erppeek library:

SUBSCRIBE TO BLOG VIA EMAIL Enter your email address to subscribe to this blog and receive notifications of new posts by email.

The next step is to tell erppeek where it can find the database and how it can access it. In order to do this we will have to tell the library the database name, the location of the database, the username and the password. Add the following code to your script, under the import:

Email Address

SU B SC R IB E

Tip: If you can you should use this script with the administrator user only, since this user has access to all models and settings. 2.2 CONNECTING TO THE DATABASE

CATEGORIES

So now that we’ve installed erppeek, we’ve imported the library and we’ve created the configuration the next step is to connect to the Odoo database in order to query and configure it. Just connect to the Odoo client:

Automated actions Buttons Chat

This line will tell your script to which server, on which database we want to connect with what user and sets the user his password. Thanks to this code we can now access the database and query it. We now just have to find the record that contains settings of this database and then do the actions we would like to execute.

Controllers Data erppeek

2.3 GETTING THE ODOO CONFIGURATION Filters

In Odoo 11 all settings are saved in the model ‘res.config.settings’ so let us get data from this model:

Github Installation

So, what exactly does this code do? It tells erppeek that we want to get data from the model ‘res.config.settings’ and that we only want to get the last record from this model (limit=1, order=’id desc’ will do that). The result will now be in the variable ‘config_id’ but there is a catch. What if this query didn’t return anything? Imagine that you have a database that you’ve just installed and you’ve never done any settings, in this case the variable config_id won’t contain any records. To prevent our script from crashing we should make an if/else statement now that handles this potential issue. Let us start with the if statement!

Mail many2many Odoo 10 Odoo 11 Odoo 8

2.4 SETTING THE NEW CONFIGURATION

Odoo 9

If we find a result we should set the changes that we want to activate and we then have to apply them on the database. Have a look at this code:

PowerBI Python Reports Security Selection

So, what exactly does this do? The first line will get the record itself, as ‘config_id’ only contains the id for the record but not the record itself. By calling the Odoo database and using the ‘browse’ function (with the ‘config_id’ passed in the call) we will get the record back. When we have this record we can write our wanted changes on this record. In this case I will configure Odoo to use different units of measures. Remember that all settings in Odoo are booleans, so we do {‘group_uom’: True} to set the boolean to true. But, where did I get the ‘group_uom’ from? This is the field of this setting. You can get this by activating the developer mode and hovering over the field:

Translations Uncategorized Webpages

ARCHIVES November 2017 October 2017 August 2017 July 2017

In this screenshot you can see that the option is not yet activated, so the boolean is set to False. {‘group_uom’: True} will change this when we apply this value. This is exactly what the last line does! By calling the record its execute function we will tell Odoo to execute this change and apply it to Odoo. If you would run this script and would go to the settings you would see that the units of measures is now activated.

April 2017 January 2017 December 2016 November 2016

2.5 SETTING THE CONFIGURATION WHEN THERE IS NO RECORD YET

October 2016 August 2016

Now we should handle one more case. When there has never been any configuration record yet we should create one. Just have a look at the following code:

April 2016 February 2016 January 2016 November 2015

September 2015

The code is almost identical to the if part (in chapter 2.4) with the minor change that we create a new and empty record with .create({}) in order to create new settings. Besides of this difference the rest of the code is identical to the if part of the code.

August 2015

2.6 RUNNING THE FILE

October 2015

Good job! You’re almost done. Now save your Python file and execute the python script to apply the setting(s) on your Odoo database. Execute the following command:

July 2015 June 2015 May 2015

After a few seconds your script will be done. Now open up your Odoo, go to the configuration and you will see that the setting has been applied to your database:

April 2015 March 2015 February 2015 December 2014

That’s it! You’ve now configured your own Odoo database settings with a simple Python script!

3. Conclusion Thanks to the erppeek library you can easily configure app settings from a Python script. With this library you can automate your databases its settings in a matter of seconds. If you would like to learn more about erppeek and the options it offers you should have a look at the official documentation. 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!

Share this: Share

Like this: Loading...

Graph OLTP & analytics

Installing Odoo 11 (enterprise) on Ubuntu OCTOB E R 8, 2017

Y E NTHE 666

53 COMME NTS

In this tutorial I will learn you how to install Odoo 11 community or enterprise on Ubuntu 16.04. The script that you will use is based on the code from André Schenkels but has been updated, upgraded and improved. Do notice that if you want to install the enterprise version that you will need to be an official partner or that you need to have bought the enterprise subscription from Odoo. Otherwise you will have no access to the Github repository for the enterprise code!

1. Downloading the script The first step is to download my script from Github and to add the code in a new .sh file on your Ubuntu machine, wherever you’d like this. For example right under /home. Open up an Ubuntu terminal and cd to the directory where you’d like to keep the script and then create the file:

If you’re curious about how the whole code looks and works you can find it on my Github account. Now open up the file and edit the parameters to your liking:

There are some things you can configure/change to your likings at the top of the script. You can choose if you wish to install Wkhtmltopdf or not, which version you’d like, where the location is and most importantly what the master admin password is. Tip: always modify this for every Odoo you install! If you want the enterprise version of V11 you should change the line IS_ENTERPRISE to true:

If you want the community version you can just continue and keep the IS_ENTERPRISE key on “False” (which is the case by default):

2. Making the Odoo installation file executable The next step is to make this file executable. After you’ve made it executable you can execute it and everything will be installed automatically. do this with the following command:

3.Running the script Now that the code is in your file and the file is executable you simply have to execute it with the following command:

You will see that the script automatically starts updates, downloads required packages, creates the user, downloads the code from Github, … Eventually, if you’ve chosen to install the enterprise version, you will need to give in your Github credentials to download the enterprise code (since this is a private repository). Fill in your details and let the script continue:

Give the script a few minutes to configure and install everything and eventually you will see something like this:

You now have a fully functional Odoo V11 community or enterprise on your system! Congratulations.

4. Extra information about Odoo 11 Enterprise Since Odoo Enterprise uses code from both http://github.com/odoo/odoo and http://github.com/odoo/enterprise we will separate the code with this script. This will make future upgrades easier and the code is nicely separated. This means that the default V11 code will be under /odoo/odooserver/ and all the enterprise code will be under /odoo/enterprise/. In the script you saw there was an option to change the Odoo port (OE_PORT). When you’d change this port number to 8070 in the install script it would be applied to /etc/your-config-file.conf and this would give you the ability to change the default port. To apply these changes you should do the following:

The -c will change the configuration and memorize what you’ve changed under /etc/your-config-file.conf. Because my port was set to 8070 this is telling the Odoo that it should run on port 8070. When you would now open up your browser and navigate to http://localhost:8070/ you will see it is running there:

Has this tutorial helped you, do you have any feedback or questions? Post away!

Share this: Share

Like this: Loading...

IN ST A L L A T IO N

IN ST A L L A T IO N SC R IPT

O D O O 11

Installing and configuring Odoo on DigitalOcean OCTOB E R 1, 2017

Y E NTHE 666

6 COMME NTS

In this tutorial I will teach you how to install Odoo on a new DigitalOcean VPS. You will learn how to create a new account, create a new server (droplet) and how to install any Odoo version on it. In this tutorial I will show you step by step how to create the server and how to install the Odoo thanks to my installation scripts.

1. Creating a DigitalOcean account If you do not have a DigitalOcean account yet you should create one. If you do you can continue to chapter 2. You can create a new DigitalOcean account through this link, which will give you $10 for free to start with. After clicking on this link you will see the following screen:

Simply click on the link, fill in an e-mailadress and a password and click on “Sign Up”. After doing so you’ll get an e-mail in your inbox. Open up the link in your e-mail and your account will be activated.

2. Creating a new server (droplet) Now that you have an account it is time to create a VPS. DigitalOcean calls a VPS (or a server) a droplet. When you’re on the home screen after logging in you’ll see a button “Create”. After you click on this button you will see a dropdown that has an option called “Droplets“, click on it:

After clicking on the button a new page will show up asking which OS you want to use and what specifications (with their prices) you’d like:

For this tutorial I will create an Ubuntu 16.04.3 droplet (VPS) with 2GB RAM, 2 CPU’s and 40GB storage. This will cost $20 per month at the time of writing. Choosing what server you need is up to you and depends on your requirements. Do know that a server with less than 1GB of RAM will not work out. Finally you will see some additional options, the ability to add an SSH key and to name your droplet (VPS) as you’d like:

I’d advice you to check on “Monitoring” as it has no extra costs but it allows you to configure warnings. For more information about monitoring you should read this article from DigitalOcean. Now give your droplet (VPS) a name and click on the “Create” button. A few seconds later you will see that the droplet (VPS) is being created:

That’s it! You’ve just created a new droplet and are ready to connect with it.

3. Connecting with the DigitalOcean droplet (VPS) Because you’ve just created a new droplet (VPS) you’ve gotten an e-mail in your inbox. Open up your mailbox and you will find an e-mail with all the information you need to connect to the remote server:

You can now SSH to the server with these details in order to configure the server and install Odoo. You can connect by using the Linux command ‘ssh’, which looks like this:

SSH tells Linux that you want to open up a secure connection, root is the username of the user you’re connecting with and 138.68.95.197 is the IP of the server. After entering this command you will get the question if you’re sure to continue connecting, just type yes. Now you’ll need to enter the password that is inside the e-mail you’ve recieved from DigitalOcean. Finally the server will ask you your current password and a new one, in order to change your automatically generated password to a safer one. Go ahead and change your password, after doing so you’re good to go:

4. Installing Odoo That wasn’t too bad right? Now there is just one more thing to do: install Odoo on the server! In order to install Odoo on the server you can use my installation script, which does a lot of the installation automatically. You can find an installation script for every Odoo version on Github. In this example I will install Odoo 10, but you’re free to install any other version. 4.1 DOWNLOADING THE INSTALLATION SCRIPT If you want to install Odoo 10 you can run the following command:

If you want to install Odoo 9 you can run the following command:

If you want to install Odoo 8 you can run the following command:

As a result you will get a new file (named ‘odoo_install.sh’) on your system which contains all the code to install Odoo:

4.2 CONFIGURING THE INSTALLATION SCRIPT Now open up the file and edit the parameters to your liking:

There are some things you can configure/change to your likings at the top of the script. You can choose if you wish to install Wkhtmltopdf or not, which version you’d like, where the location is and most importantly what the master admin password is. Tip: always modify this for every Odoo you install! If you want the enterprise version of V10 you should change the line IS_ENTERPRISE to true:

If you want the community version you can just continue and keep the IS_ENTERPRISE key on False (which is the case by default):

4.3 MAKING THE ODOO INSTALLATION FILE EXECUTABLE The next step is to make this file executable. After you’ve made it executable you can execute it and everything will be installed automatically. You can do this with the following command:

4.4 RUNNING THE INSTALLATION SCRIPT Now that the code is in your file and the file is executable you simply have to execute it with the following command:

You will see that the script automatically starts updates, downloads required packages, creates the user, downloads the code from Github, … Eventually, if you’ve chosen to install the enterprise version, you will need to give in your Github credentials to download the Enterprise code (since this is a private repository). Fill in your details and let the script continue:

Finally, give the script a few minutes to configure and install everything and eventually you will see something like this:

You now have a fully functional Odoo on your system! Congratulations. 4.5 ACCESSING YOUR ODOO Good job! You’ve created a DigitalOcean account, created a droplet (VPS), configured it and installed Odoo on it. Now it is time to enjoy the results of your hard work. If you now open up your browser and surf to your IP and the port you’ve installed Odoo on (by default 8069) you can see your Odoo is up and running. Because the droplet (VPS) is automatically connected to the internet (a public IP address) your Odoo will also be publicly available. Go to http://yourip:yourport and you will see your just installed Odoo:

4.6 SECURITY ADVISORY As security is a very big topic I did not add anything about security in this tutorial. Please do configure your Odoo with a firewall, Nginx, SSL and best security practices. The security part is out of scope in this tutorial because of the size of the subject but please do add in security rules. For more information please do look at Nginx, the official DigitalOcean security measures and LetsEncrypt.

5. Conclusion Creating a new server, configuring it and installing an Odoo on it is very easy with DigitalOcean. You’ll have a VPS in a matter of minutes for an affordable price. Due to the accessibility and flexibility of DigitalOcean and Odoo it has never been so easy to have an ERP publicly available. Has this tutorial helped you, do you have any feedback or questions? Post away!

Share this: Share

Like this: Loading...

D IG IT A L O C EA N

D R O PL ET

IN ST A L L A T IO N

O D O O 10

O D O O 9

Extend existing selection with selection_add in Odoo 10 A UGUS T 28, 2017

Y E NTHE 666

LE A V E A COMME NT

In this tutorial I will teach you how to extend an existing selection through code in Odoo 10. You will learn how to extend the selection (without overriding the existing keys) and only adding new keys with the “selection_add” parameter. In this tutorial I will extend the existing selection for an employee where the gender is set. After this toturial you will be able to add new keys to a selection.

1. Adding the correct dependencies Alright let us start right away! First of all you will need to add a dependency to the module from which you want to extend the selection. In this tutorial I will need the “hr” module and I’ll add in the “contacts” module too because you’ll want to see the contacts anyway. Open up your __manifest__.py and add in the dependencies you need:

2. Inheriting the model Now create a new Python file that inherits the model on which you want to extend an existing selection. In my example I’ll need to inherit ‘hr.employee’ as the field ‘gender’ is on this model:

Your code should now look like this:

3. Extending the selection Finally (or should I say already?) you can extend the field and add the new options you’d like. Inheriting the existing field is as simple as calling it with the same name again. Then just use the “selection_add” key to insert new options. Your code should be looking like this:

So what does this code do? Rather then having to give all key values again you can simply add only the new values you’d like. Odoo will automatically add these values to the selection without modifying anything to the existing keys. As a result this is the cleanest way to extend selections and prevents you from reassigning values every time. Your final code should now look like this:

After you’ve installed this custom code you’ll see that the new value is added because of this code:

4. Conclusion Extending selections in Odoo has become very easy due to the “selection_add” parameter. It allows you to extend selections without having to override existing keys and removes some complexity and margin for errors. 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!

Share this: Share

Like this: Loading...

O D O O 10

SEL EC T IO N

SEL EC T IO N _A D D

Creating webpages and controllers in Odoo 10 J ULY 15, 2017

Y E NTHE 666

5 COMME NTS

In this tutorial I will teach you how to create new webpages and controllers through code in Odoo 10. You will learn how to create new webpages (in XML), how to create controllers and how to use controllers to open specific webpages. After following this tutorial you will be able to create your own webpages and controllers in Odoo. In this example I will create a webpage with a button. The button click will call a controller function and this controller will redirect you to another webpage. On this page we will show the details of all the companies in your Odoo:

1. Creating the controller structure Before you can create (and call) a webpage you need to make a controller. This controller will tell Odoo which URL links to which webpage. Open up your module and create a folder ‘controllers’ if you don’t have one yet. After creating this folder make an __init__ file and add the followig line of code in it:

Now that you have an import that is directly loaded by Odoo (thanks to the __init.py__) you should create a new Python file. Name this new file ‘example.py’, which will be the controller. As a result your folder structure should be looking like this:

1.1 CREATING THE CONTROLLER Let us create our first controller now! Just have a look at this code:

So, what does this tell you? In order to let an user navigate to a specific page you will need an @http.route. This @http.route will tell Odoo that we want to link the url ‘/example’ to a specific webpage. Inside the @http.route you will see four variables. Let me explain them one by one: ‘/example’: Because Odoo needs to know which URL links to what (XML) template you’ll need to provide the URL first. In this example it means that http://localhost/example would call this @http.route type=’http’: This will let Odoo know that your page is running in HTTP. auth=’public’: Tells Odoo who can view the page. You can choose between ‘public’, ‘user’ and ‘none’. If you choose ‘public’ anybody can view the page. When you choose ‘user’ only logged in people can view this page and if you choose ‘none’ there will be no facilities to access the database or any configuration. For more information see the official documentation website=True: Tells Odoo that is has to link to a webpage and that it is not just a Python function. After calling @http.route you will need to give the Python function a name, in this example render_example_page(self). Notice that self is also passed along in functions in the front-end of Odoo, just like in the backend. 1.2 RETURNING THE CONTROLLER FUNCTION Finally, you need to return the function. Because the function needs to know which XML record needs to be called we pass it along like this:

http.request.render will call the view renderer from the Odoo framework. within the brackets “()” you have to specify the module_name.page_name. In this case my module is named ‘create_webpage_demo’ and I will create a webpage with the technical name ‘page_name’. In this http.request.render you’ll also see {}. Why is this there? You can pass variables and data along in this dictionary to the website. Further in this tutorial I will show an example about how to do this, so don’t worry about it yet.

2. Creating the webpage Close the Python file, go into the ‘views’ folder and create a new XML file named ‘example_webpage.xml’. Add this XML file in your manifest.py first:

Now go back to your example_webpage.xml file and create a new XML record. You’ll notice that creating a new webpage is just as easy as creating an Odoo view. Have a look at this code first and then add it in your module:

Does this make sense to you? Let me explain it step by step. The record starts with a template id, which is identical to creating views in Odoo. Why did I name this example_page though? Because in the controller we did this: return http.request.render(‘create_webpage_demo.example_page‘, {}). As a result Odoo will now know that the controller needs to render the XML view that we’re now coding. Did you notice the page=”True” key too? Since Odoo needs to know if it is a webpage or not there is a key ‘page’ added in Odoo. By settings this key to true Odoo knows it’ll become a webpage. Next, we have the following line:

Because of this Odoo knows that it has to take the default webpage lay-out and needs to apply it to this XML record. The next line has some noteworthy features. The div class oe_structure will add the feature to use building blocks within Odoo, which is otherwise not possible. As a result other people, that use your Odoo that do not have technical knowledge, will be able to use building blocks to style the webpages. As for the rest of the code? You could basically add what you want here. Because Odoo implements bootstrap, less and font awesome you can use a lot of features out of the box! 2.1 SAVING AND SHOWING THE WEBPAGE Finally, save this page, update your module and open your Odoo. In the website go to /example and you’ll see the following result:

Do you see that button? In the XML record that we just created I added the following line of code:

I’ve added a t-attf-href in this line with a reason, but why? Because the Odoo framework will be triggered the moment that you click on this button. Odoo can interpret this code and knows that you want to open the page /example/detail. So, let us continue and let us create another function in the controller to handle this. 2.2 SECOND FUNCTION IN CONTROLLER Open up the Python file ‘example.py’ again and let us create a second function, just like we did with the first. There will be just one addition to the code, we’ll also pass data to the next webpage! Go ahead and create a second function that links to /example/result. Your code should look like this:

This code will work fine and you’ll be able to open the page /example/detail (if you’d have an XML record by now too), but, we still need to pass data along. 2.3 PASSING DATA TO THE WEBPAGE Since there is no code to pass data along yet we should add it. You can call data from the database in controllers too, it just works slightly different. Usually in Odoo you would do this:

It’s not possible to do it like this in the controller though. In the controller we will need to use ‘http.request.env’ to fetch data from a model. As a result your code will look like this:

I’ve also added the function “.sudo()” on this search. Why? Since the website looks at user rights and access rights too you’d need to make sure the users have enough rights. As this is not in the scope of this tutorial I simply add “.sudo()” so that the database query is executed as if it was done by the super administrator user without any security problems. The final thing to do is to pass this data, that is in the variable companies, to the webpage. How do you do that? Like this:

Now you’re all set on the controller side to open up the page /example/detail and to pass all data to the webpage. The final code of your function will look like this:

Because of doing {‘companies’: companies} in the dictionary all data from all companies will be passed along to the webpage. Save this code and let us create the second XML record for the webpage in ‘example_webpage.xml’. 2.5 CREATING THE DETAIL VIEW Finally, let us create the second webpage and then we’re all done! Open up the ‘example_webpage.xml’ file and create a second XML record, where you also set the page=”True” key on. On this page we will show all company details in a table, which is passed along in the variable “companies”. Have a look at this code and then add it in your XML file:

The controller will know that it has to call this record thanks to the name “detail_page” so when you would save this and update your module everything will work. Since we’ve passed along the dictionary companies we can access all company data. To print all records (in multi company you can have multiple companies) we loop over the records thanks to the t tforeach code:

Because you’re doing a foreach you can access every company it’s data with the variable “company” later on. As a result we can access any data from the company with “company.field_name”. You can show the data in the webpage with span t-esc=”company.field_name” to print it out:

Finally, save this file, update your module and go to /example/detail. You will now see the following result:

3. Conclusion Creating webpages and using controllers in Odoo is quite easy. As the Odoo framework has a lot of built in libraries (such as requests, less, bootstrap, font awesome, …) it has never been as easy to create webpages as in Odoo 10. As a result you can create beautiful webpages with any data you want in record times. 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!

Share this: Share

Like this: Loading...

C O N T R O L L ER

WEB PA G E

WEB SIT E

Override create functions in Odoo A P RI L 7, 2017

Y E NTHE 666

2 COMME NTS

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:

The result in the database:

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!

Share this: Share

Like this: Loading...

C R EA T E

O VER R ID E

SU PER

P ROUDLY P OWE RE D BY WORDP RE S S | THE ME : FI CTI V E BY WORDP RE S S . COM.

Smile Life

When life gives you a hundred reasons to cry, show life that you have a thousand reasons to smile

Get in touch

© Copyright 2015 - 2024 PDFFOX.COM - All rights reserved.