5 Python Mistakes I Still See in Odoo Customisations
None of these mistakes will break your module today. All of them will break something — performance, security, or the next upgrade — eventually.

In my first article I wrote that the tools change but the problems don't. Nowhere is that truer than in Odoo customisations.
I have reviewed, inherited, and — let me be honest — written my share of Odoo code over the years. The same five mistakes keep appearing, in module after module. None of them will break your code today. That is exactly why they survive: they work in testing, with ten records and one user, and then they fail slowly in production, with ten thousand records and fifty users.
Here they are, each with a short example. All examples are generic demos — the lesson is what matters, not any real system.
1. Business logic in the wrong layer
The most common version: an important business rule lives inside a button method, an onchange, or a controller — copied slightly differently in each place that needs it.
# In a button method, an onchange, AND a controller — three copies, three futures
if order.amount_total > 10000000 and not order.x_approved_by:
raise UserError("This order needs approval first.")The problem is not the rule. The problem is that the rule has three homes. Next year someone changes one copy and forgets the other two, and now the same order is blocked on screen but allowed through the import.
Better: one rule, one home — on the model, where every caller shares it.
def _check_approval(self):
for order in self:
if order.amount_total > 10000000 and not order.x_approved_by:
raise UserError("This order needs approval first.")Buttons, onchanges, and controllers should call business logic, not be business logic.
2. Queries inside loops
This one works perfectly — until it meets real data.
# One search per line. 10 lines = 10 queries. 1,000 lines = 1,000 queries.
for line in order.order_line:
taxes = self.env['account.tax'].search([
('type_tax_use', '=', 'sale'),
('company_id', '=', line.company_id.id),
])The developer tested with a five-line order. Production runs a five-hundred-line import, and the screen that took two seconds now takes two minutes. Nothing is "broken". It is just doing the same query hundreds of times.
Better: fetch once, before the loop, and work in memory.
all_taxes = self.env['account.tax'].search([
('type_tax_use', '=', 'sale'),
('company_id', 'in', order.order_line.company_id.ids),
])
taxes_by_company = all_taxes.grouped('company_id')
for line in order.order_line:
taxes = taxes_by_company.get(line.company_id, self.env['account.tax'])Odoo's ORM is built around recordsets. If you are querying inside a loop, you are probably fighting the framework instead of using it. I learned this first as a database administrator, long before Odoo: check what you are asking the database to do, and how many times you are asking it.
3. sudo() everywhere
sudo() is sometimes necessary. It is also the easiest way to silently delete your security model.
# "It raised an AccessError, so I added sudo()" — the most dangerous sentence in Odoo
invoice = self.env['account.move'].sudo().browse(invoice_id)
invoice.action_post()That AccessError was not a bug. It was Odoo telling you this user should not be able to do this. Bypassing it means any user who can reach this code can now post invoices — including, one day, a user who really shouldn't.
Better: first ask why the access failed. Usually the honest fixes are a group, a record rule, or a narrower sudo() on exactly the one operation that legitimately needs it — with a comment saying why:
# sudo justified here: posting is done by the system during a scheduled
# reconciliation, as no interactive user. Access checked above by group.
if not self.env.user.has_group('account.group_account_manager'):
raise AccessError("Only accounting managers can run this.")
invoice.sudo().action_post()Rule of thumb: if you can't write the comment explaining why sudo() is safe, it probably isn't.
4. Hard-coded IDs
# Works on your database. Breaks on every other database.
warehouse = self.env['stock.warehouse'].browse(1)
journal = self.env['account.journal'].browse(7)IDs are local facts about one database. A fresh install, a restored backup, a new company, an upgrade — any of these can give your warehouse a different number, and your code will calmly pick the wrong record instead of failing loudly. That is the worst kind of bug: silent and plausible.
Better: refer to records by something that means the same thing everywhere — an XML ID, or the current company:
warehouse = self.env['stock.warehouse'].search([
('company_id', '=', self.env.company.id),
], limit=1)
main_warehouse = self.env.ref('stock.warehouse0') # standard data, stable nameIf a record matters enough to name in code, give it a name that survives the journey to another database.
5. No test for the edge case the user will find
Developers test the happy path: a normal order, a normal quantity, a normal user. Users are more creative. They will, sooner or later:
- save a record with the optional field empty,
- enter a quantity of zero,
- cancel the document your code assumed was confirmed,
- run your wizard on no records at all.
# Crashes the day someone runs it on an empty selection,
# and divides by zero the day a quantity is 0.
average = total / order.order_line[0].product_uom_qtyBetter: handle the boring cases first, in the open, before the interesting logic:
lines = order.order_line.filtered('product_uom_qty')
if not lines:
return # nothing meaningful to compute yet — and that's fine
average = total / sum(lines.mapped('product_uom_qty'))You do not need a test suite worthy of a framework. You need one honest pass where you ask: what does this do with nothing, with zero, and with the record in the wrong state? Five minutes of that has saved me more weekends than any clever optimisation.
The pattern behind all five
Look at the list again. Not one of these is a syntax error. Not one is about being a "bad programmer". Every one is a shortcut that traded a small saving today for a larger cost later — in performance, in security, in upgrades, or in a weekend spent debugging.
Senior development, I have come to think, is mostly the discipline of refusing that trade when you can see it coming. The code you write this month will be maintained by someone — quite possibly a tired future version of you. Write the version they will thank you for.
Your turn: which of these five have you met most often — or written yourself? I have done at least three of them. I'll leave you to guess which.

Discussion
No comments yet