No more print()
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 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.
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.
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.
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.
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.
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.
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.
When Odoo reads that line, it will pause the program for a moment until you click Resume.
When it paused, PyCharm will show this Debug Window and you can inspect the variables that have been computed just before it breaks.
Last, I can watch only some variables when they meet my conditions.
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)