{"id":9147,"date":"2024-09-17T07:15:24","date_gmt":"2024-09-17T07:15:24","guid":{"rendered":"https:\/\/www.infinitivehost.com\/knowledge-base\/?p=9147"},"modified":"2024-09-17T07:36:19","modified_gmt":"2024-09-17T07:36:19","slug":"filter-journals-in-odoo-with-coding-a-simple-guide","status":"publish","type":"post","link":"https:\/\/www.infinitivehost.com\/knowledge-base\/filter-journals-in-odoo-with-coding-a-simple-guide\/","title":{"rendered":"Filter Journals in Odoo with Coding: A Simple Guide"},"content":{"rendered":"<div class='epvc-post-count'><span class='epvc-eye'><\/span>  <span class=\"epvc-count\"> 1,770<\/span><span class='epvc-label'> Views<\/span><\/div>\n<p>Filtering journals programmatically in Odoo involves using Odoo&#8217;s ORM (Object-Relational Mapping) methods in Python. This is typically done in custom modules or scripts. Here&#8217;s a step-by-step guide on how to filter journals using code:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. Setting Up Your Development Environment<\/strong><\/h3>\n\n\n\n<p>Ensure you have access to Odoo\u2019s development environment and that you can run Python scripts or modify Odoo modules. This might involve:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Access to Odoo Server<\/strong>: Make sure you have access to the Odoo server where your database is hosted.<\/li>\n\n\n\n<li><strong>Development Setup<\/strong>: Have a development environment set up where you can test your changes (e.g., a local Odoo instance).<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. Filtering Journals in Python Code<\/strong><\/h3>\n\n\n\n<p>You can filter journals using the Odoo ORM in a Python script. Below is an example of how you can do this:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Example Script Using Odoo Shell<\/strong><\/h4>\n\n\n\n<p>If you are using the Odoo shell (accessed via command line), you can execute the following code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\"># Start the Odoo shell\nodoo shell -d your_database_name\n\n# Then execute the following code in the Odoo shell\njournals = env&#91;'account.journal'].search(&#91;('type', '=', 'bank')])  # Filter journals of type 'bank'\nfor journal in journals:\n    print(journal.name)<\/mark><\/code><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Example in a Custom Module<\/strong><\/h4>\n\n\n\n<p>If you want to include this in a custom module, you would typically write it in a method of a model or in a server action. Here\u2019s an example of how you might do this in a custom module:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Create a Custom Module:<\/strong><br>Set up a new custom module or use an existing one.<\/li>\n\n\n\n<li><strong>Define a Method to Filter Journals:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   <code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">from odoo import models, api\n\n   class YourModel(models.Model):\n       _name = 'your.model'\n       _description = 'Your Model Description'\n\n       @api.model\n       def filter_journals(self, journal_type='general'):\n           # Search for journals based on type\n           journals = self.env&#91;'account.journal'].search(&#91;('type', '=', journal_type)])\n           return journals<\/mark><\/code><\/code><\/pre>\n\n\n\n<p>In this example, <code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">filter_journals<\/mark><\/code> method filters journals based on their type (<code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">journal_type<\/mark><\/code> parameter) and returns the result.<\/p>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>Use the Method:<\/strong> You can call this method from various places in Odoo, such as:<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Button Click<\/strong>: In a form view or tree view, trigger this method via a button.<\/li>\n\n\n\n<li><strong>Scheduled Actions<\/strong>: Use this method in automated scheduled actions.<\/li>\n<\/ul>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>Update Your Module:<\/strong> After writing your code, update your module to apply the changes. You can do this through the Odoo UI or by running:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   odoo -u your_module_name<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. Example with XML-RPC API<\/strong><\/h3>\n\n\n\n<p>If you prefer to filter journals using an external script, you can use XML-RPC to connect to Odoo. Here\u2019s an example using Python:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">import xmlrpc.client\n\n# Define connection parameters\nurl = 'http:\/\/your-odoo-url'\ndb = 'your-database'\nusername = 'your-username'\npassword = 'your-password'\n\n# Connect to Odoo\ncommon = xmlrpc.client.ServerProxy('{}\/xmlrpc\/2\/common'.format(url))\nuid = common.authenticate(db, username, password, {})\nmodels = xmlrpc.client.ServerProxy('{}\/xmlrpc\/2\/object'.format(url))\n\n# Filter journals where type is 'bank'\njournal_type = 'bank'\ndomain = &#91;('type', '=', journal_type)]\nfields = &#91;'name', 'type']\njournals = models.execute_kw(db, uid, password, 'account.journal', 'search_read', &#91;domain], {'fields': fields})\n\n# Print filtered journals\nfor journal in journals:\n    print(journal)<\/mark><\/code><\/code><\/pre>\n\n\n\n<p>In this script, the <code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">domain<\/mark><\/code> variable defines the filter criteria for the journals.<\/p>\n\n\n\n<p><strong>Conclusion<\/strong><\/p>\n\n\n\n<p>Filtering journals with the help of coding in Odoo consists of utilizing Odoo\u2019s ORM (Object-Relational Mapping) ways in the case of Python. This is usually done in the section of scripts or custom modules. Here is a complete guide on how to filter journals with the use of code by having <a href=\"https:\/\/www.infinitivehost.com\/managed-odoo-server-solutions\"><mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\"><strong>managed Odoo server solutions<\/strong><\/mark><\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1,770 Views Filtering journals programmatically in Odoo involves using Odoo&#8217;s ORM (Object-Relational Mapping) methods in Python. This is typically done in custom modules or scripts. Here&#8217;s a step-by-step guide on how to filter journals using code: 1. Setting Up Your Development Environment Ensure you have access to Odoo\u2019s development environment and that you can run [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[203],"tags":[],"class_list":["post-9147","post","type-post","status-publish","format-standard","hentry","category-odoo"],"_links":{"self":[{"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/posts\/9147","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/comments?post=9147"}],"version-history":[{"count":2,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/posts\/9147\/revisions"}],"predecessor-version":[{"id":9152,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/posts\/9147\/revisions\/9152"}],"wp:attachment":[{"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/media?parent=9147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/categories?post=9147"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/tags?post=9147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}