Odoo Format Fields#
Suppose you have a simple Integer field defined on your model like this:
class Lead(models.Model):
number = fields.Integer('Number')
To display it in the form, you would have a view like this:
<field name="number" widget="integer" options="{'format': False}"/>
But for larger numbers like 12345
, Odoo would format them according to your
locale, e.g. 12,345
. This is bad UX, if you want to copy & paste this number.
Here is how to remove the format:
<field name="number" widget="integer" options="{'format': False}"/>
Simple, right?
But how do you find out what kind of options are there?
Finding nodeOptions
#
First, you have to find the widget’s implementation in
addons/web/static/src/legacy/js/fields/field_registry.js
In our example that’s the line
.add('integer', basic_fields.FieldInteger)
which is defined in addons/web/static/src/legacy/js/fields/basic_fields.js
as:
var FieldInteger = NumericField.extend({
So we look at NumericField
and find the following by searching for
nodeOptions
:
init() {
this._super.apply(this, arguments);
this.shouldFormat = Boolean(
JSON.parse('format' in this.nodeOptions ? this.nodeOptions.format : true)
);
},
This tells us, that there is a boolean option format
, that we can set to
False as we did before:
<field name="number" widget="integer" options="{'format': False}"/>
Using Pycharm’s Odoo Addon#
At this point I’d like to give a shout out to the Odoo Framework Integration for PyCharm Plugin by Trịnh Anh Ngọc. It’s well worth its price. With it, here’s what you can do:
in your view, ctrl+click the
integer
word ofwidget='integer'
ctrl+click
FieldInteger
ctrl+click
FieldInteger
againctrl+click
NumericField
search for
nodeOptions
And there you have it.