Adding a Custom Column in Odoo 18 General Ledger
Odoo 18’s accounting module is a powerful tool for managing financial transactions and generating reports. Among its many features, the General Ledger (GL) report is essential for businesses, offering a comprehensive view of all financial entries. But what if your business requires additional details in the report that are not included by default?
Imagine being able to add a custom column to the General Ledger, displaying precisely the information you need to streamline your financial analysis. For instance, you might want to include an “Opportunity ID” to track which sales opportunities each transaction is linked to.
This guide will walk you through adding a custom column to the General Ledger in Odoo 18. Whether you’re new to Odoo or an experienced user, this step-by-step approach ensures you can easily enhance your financial reports to meet your unique business requirements. Let’s explore how to customise the Odoo 18 General Ledger and take your reporting capabilities to the next level!
How to Add a Custom Column in Odoo 18 General Ledger: A Step-by-Step Guide
Step 1: Identify the Field to Add
Before customizing your Odoo 18 General Ledger, decide on the additional information you want to display in the report. For this example, let’s say we want to add an “Opportunity ID” field to categorise the transactions.
This could be particularly useful if you want to track which opportunities each transaction is linked to, offering a more detailed view of your financial data.
Step 2: Inherit the General Ledger Model
Odoo uses a modular structure, allowing us to extend existing models without changing core files. To add a custom column to the General Ledger, we need to inherit the account.move.line model and define the new field.
Here’s how to do it:
from odoo import models, fields
class AccountMoveLine(models.Model):
_inherit = 'account.move.line'
lead_reference = fields.Char("Opportunity ID", readonly=True, copy=False)
- lead_reference is the field name that will store the “Opportunity ID.”
- “Opportunity ID” is the label displayed in the General Ledger.
Step 3: Modify the General Ledger Report (XML)
Next, we need to modify the QWeb report template for the General Ledger. This step adds the custom field to the report layout, ensuring the new column is displayed.
Find the General Ledger report template in account_reports and add it to your custom module:
Here, inheriting the template may not give you the desired result. So declaring the new field lead_reference like the default fields in the base code is enough, and are as follows:
General Ledger
Opportunity ID
lead_reference
string
This will add a new column at the end of the General Ledger table. You can rearrange the position of the latest or the existing column from the front end itself.
Step 4: Modify the General Ledger Report (Python Code)
Let’s get into the Python code to customize the SQL query that fetches account move lines (AMLs). The key customisation here is adding the lead_reference custom field to the SQL query.
The following code snippet modifies the SQL query to include the new lead_reference field in the General Ledger report:
class AccountGeneralLedger(models.AbstractModel):
_inherit = 'account.general.ledger.report.handler'
def _get_query_amls(self, report, options, expanded_account_ids, offset=0, limit=None) -> SQL:
additional_domain = [('account_id', 'in', expanded_account_ids)] if expanded_account_ids is not None else None
queries = []
journal_name = self.env['account.journal']._field_to_sql('journal', 'name')
for column_group_key, group_options in report._split_options_per_column_group(options).items():
query = report._get_report_query(group_options, domain=additional_domain, date_scope='strict_range')
account_alias = query.join(lhs_alias='account_move_line', lhs_column='account_id',
rhs_table='account_account', rhs_column='id', link='account_id')
account_code = self.env['account.account']._field_to_sql(account_alias, 'code', query)
account_name = self.env['account.account']._field_to_sql(account_alias, 'name')
account_type = self.env['account.account']._field_to_sql(account_alias, 'account_type')
query = SQL(
'''
SELECT
account_move_line.id,
account_move_line.date,
account_move_line.date_maturity,
account_move_line.name,
account_move_line.ref,
account_move_line.company_id,
account_move_line.account_id,
account_move_line.payment_id,
account_move_line.partner_id,
account_move_line.currency_id,
account_move_line.amount_currency,
COALESCE(account_move_line.invoice_date, account_move_line.date) AS invoice_date,
%(debit_select)s AS debit,
%(credit_select)s AS credit,
%(balance_select)s AS balance,
move.name AS move_name,
company.currency_id AS company_currency_id,
partner.name AS partner_name,
move.move_type AS move_type,
%(account_code)s AS account_code,
%(account_name)s AS account_name,
%(account_type)s AS account_type,
journal.code AS journal_code,
%(journal_name)s AS journal_name,
full_rec.id AS full_rec_name,
%(column_group_key)s AS column_group_key,
account_move_line.lead_reference AS lead_reference -- 🔹 Added custom field
FROM %(table_references)s
JOIN account_move move ON move.id = account_move_line.move_id
%(currency_table_join)s
LEFT JOIN res_company company ON company.id = account_move_line.company_id
LEFT JOIN res_partner partner ON partner.id = account_move_line.partner_id
LEFT JOIN account_journal journal ON journal.id = account_move_line.journal_id
LEFT JOIN account_full_reconcile full_rec ON full_rec.id = account_move_line.full_reconcile_id
WHERE %(search_condition)s
ORDER BY account_move_line.date, account_move_line.move_name, account_move_line.id
''',
account_code=account_code,
account_name=account_name,
account_type=account_type,
journal_name=journal_name,
column_group_key=column_group_key,
table_references=query.from_clause,
currency_table_join=report._currency_table_aml_join(group_options),
debit_select=report._currency_table_apply_rate(SQL("account_move_line.debit")),
credit_select=report._currency_table_apply_rate(SQL("account_move_line.credit")),
balance_select=report._currency_table_apply_rate(SQL("account_move_line.balance")),
search_condition=query.where_clause,
)
queries.append(query)
full_query = SQL(" UNION ALL ").join(SQL("(%s)", query) for query in queries)
if offset:
full_query = SQL('%s OFFSET %s ', full_query, offset)
if limit:
full_query = SQL('%s LIMIT %s ', full_query, limit)
return full_query
Step 5: Update and Restart Odoo
Make sure you have updated the __init__.py and __manifest__.py with the python file and xml file, respectively.
Upgrade the module and restart the server to apply changes.
Final Thoughts on Adding a Custom Column in GL
Following these steps, you have successfully added a custom column to the Odoo 18 General Ledger. This customisation allows businesses to enhance their financial reporting based on specific needs. You can further extend this by adding filters, exporting capabilities, or additional fields.
Would you like more enhancements on this customisation? Let’s contact us now!



