如何在Odoo中继承或重写JS文件?修改指定函数参数值
name_search Limit in form_common.js) Hey there! Great question—you absolutely can override the get_search_result function in Odoo's form_common.js to adjust that name_search limit parameter. Let's walk through the exact steps to do this properly (without modifying core Odoo files, which is a critical best practice to keep your setup upgrade-safe).
Step 1: Set Up a Custom Module
First, you'll need a custom module to house your changes. If you don't have one already, create a basic module structure like this:
your_custom_m2o_override/ ├── __init__.py ├── __manifest__.py └── static/ └── src/ └── js/ └── override_m2o_search.js
The __init__.py can be empty, and __manifest__.py will define your module's metadata and assets.
Step 2: Write the JS Override Code
In the override_m2o_search.js file, we'll use Odoo's include method to extend the FieldMany2One widget (since the get_search_result function belongs to this widget in the web module). Here's the code to replace the 160 limit with your desired value (I'll use 200 as an example):
odoo.define('your_custom_m2o_override.override_m2o_search', function (require) { "use strict"; // Import the core widget we need to extend var FieldMany2One = require('web.FieldMany2One'); // Override the get_search_result function FieldMany2One.include({ get_search_result: function (search_val) { var self = this; // Replicate the original logic but adjust the limit parameter return this._rpc({ model: this.field.relation, method: 'name_search', args: [search_val, this.get_domain(), 'ilike', this.get_context()], // Replace 200 with your preferred limit value here kwargs: {limit: 200}, }).then(function (results) { self.last_search = results; return results; }); }, }); return FieldMany2One; });
If you prefer to keep most of the original function intact and just tweak the limit, you could also use this._super to call the original method and modify the arguments, but for a small change like this, reimplementing the function is straightforward.
Step 3: Update Your Module Manifest
Open __manifest__.py and add your JS file to the backend assets so Odoo loads it when the backend runs:
{ 'name': 'Custom Many2One Search Limit Override', 'version': '1.0', 'depends': ['web'], 'data': [], 'assets': { 'web.assets_backend': [ 'your_custom_m2o_override/static/src/js/override_m2o_search.js', ], }, 'installable': True, 'auto_install': False, }
Step 4: Install & Test the Module
- Go to Odoo's Apps menu, search for your custom module, and install it (or upgrade it if it's already installed).
- Clear your browser cache—this is crucial because Odoo caches JS assets, so you might not see changes otherwise.
- Test any Many2One field: open the search dropdown, and you can verify the new limit by checking the network tab in your browser's dev tools (look for the
name_searchAPI call, thelimitparameter should now be your chosen value).
Key Notes
- Never edit core files: Modifying
addons/web/static/src/js/views/form_common.jsdirectly will break when you upgrade Odoo, and can cause conflicts with other modules. Using a custom module is the supported way to make these changes. - Version compatibility: This approach works across most modern Odoo versions (12+). If you're on an older version, the widget path might differ slightly, but the
includepattern remains the same.
内容的提问来源于stack exchange,提问作者HVH

