Debugging Odoo

No more print()

Amirul M.
Amirul M.
Aug 16, 2020 · 4 min read
Image for post
Image for post
Rubber Duck Debugging. Photo by Timothy Dykes on Unsplash

For you that have been work in a computer-related field, certainly feel familiar with debugging term. It is the process of finding where and how bugs ‘accidentally’ created in the system so then we can resolve it. We can use the manual way or the automatic way. For the latter, there are so many tools to do it, usually be distinguished by its programming language base.

In Odoo (that uses Python as its base), there are few tricks we can use.

Odoo Debug

Odoo has provided us a series of tools like: check metadata of a record, view UI (form, list) id, showing field name while hovering until it can show some part of the XML code that responsible for viewing. All of that can be accessed by activating debugging mode. Go to Setting -> Activate the developer mode.

Image for post
Image for post
Or we can just type ‘debug’ at the end of the URL like localhost:8069/web?debug

After activating, in the right-top corner, we can see the database that we use and there is a bug logo that shows now. There are a lot of features inside it.

Image for post
Image for post
Differences after activating a debugging mode

From this image, we can see that there are testing features, superuser, and etc. But I only use 2 most useful features: View Metadata and Edit View. I use View Metadata when I need to find record id and when its created or updated.

Image for post
Image for post
List of features

And I use Edit View when I need to find a model (database name), External id (XML), and I can edit UI (form, tree, or kanban) directly from it. But for a longer period, I suggest you shouldn’t use this method. Instead, inherit its External id, and then write your custom view. How? I write it in my past story.

Image for post
Image for post
Example of ‘Edit View’ Feature

The last existing feature I often use is the hovering field. When I do, I can see its name, object, type, context, domain, modifiers, and its relation. Fyi, the field name is the name we used in the column of the object database. And modifiers is its behavior, like when it should how and when it’s not.

Image for post
Image for post
Hovering on field

PyCharm Debug

When I code Odoo, I use PyCharm IDE because of its complete pack. I can manage my git from there, I can watch my files, and the last, I can debug and console my code in particular lines of file! So, how can I debug in this IDE? Just click the bug button in the header.

Image for post
Image for post
Debug button (third item from left)

Then, for example, I want to debug my create method, so I place my breakpoint (red circle left annotation) in line 495 of stock_picking.py. Trigger it by click Create and Save button in stock picking form.

Image for post
Image for post
Breakpoint in stock_picking

When Odoo reads that line, it will pause the program for a moment until you click Resume.

Image for post
Image for post
Pause when debugging

When it paused, PyCharm will show this Debug Window and you can inspect the variables that have been computed just before it breaks.

Image for post
Image for post
Debug window

Last, I can watch only some variables when they meet my conditions.

Image for post
Image for post
Watching variables

The Last

As said of Filipe Fortes on Twitter: “Debugging is like being the detective in a crime movie where you are also the murderer.”

So I suggest you know fully about the code you write first. Or if it is other people’s code, at least you know the flow and then you will know where you should place the breakpoint. Happy debugging! (Although you’re crying inside, lol)

The Startup

Medium's largest active publication, followed by +756K people. Follow to join our community.

Amirul M.

Written by

Amirul M.

Tech writer. Currently about Odoo development

The Startup

Medium's largest active publication, followed by +756K people. Follow to join our community.

Amirul M.

Written by

Amirul M.

Tech writer. Currently about Odoo development

The Startup

Medium's largest active publication, followed by +756K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store