I have some selection field in my model. Here example:
JavaScript
x
12
12
1
class MyModel(models.Model):
2
_name = 'my_app.my_model'
3
4
example_selection = fields.Selection(
5
[
6
('first', 'First'),
7
('second', 'Second'),
8
# etc.
9
],
10
string='My selection',
11
)
12
In some cases I need hide specific options in selection(or radio buttons). How I can do this properly?
Below screen from base calendar module which can more explain about my problem.
Thanks in advance.
Advertisement
Answer
I found the solution.
First of all we need to create custom widget for FieldSelection. Here example(path_to_your_module/static/src/js/form_widgets.js):
JavaScript
1
28
28
1
odoo.define('your_module.form_widgets', function (require) {
2
"use strict";
3
4
var core = require('web.core');
5
var FieldSelection = core.form_widget_registry.get('selection');
6
7
var MySelection = FieldSelection.extend({
8
// add events to base events of FieldSelection
9
events: _.defaults({
10
// we will change of visibility on focus of field
11
'focus select': 'onFocus'
12
}, FieldSelection.prototype.events),
13
onFocus: function() {
14
if (
15
// check values of fields. for example I need to check many fields
16
this.field_manager.fields.name_field_1.get_value() == 'value1' &&
17
this.field_manager.fields.name_field_2.get_value() == 'value2' /* && etc fields...*/
18
) {
19
// for example just hide all options. You can create any kind of logic here
20
this.$el.find('option').hide();
21
}
22
}
23
});
24
25
// register your widget
26
core.form_widget_registry.add('your_selection', MySelection);
27
});
28
After this you need just set your widget to field in your view like this:
JavaScript
1
2
1
<field name="example_selection" widget="your_selection"/>
2
If you don’t know how to include static of your module HERE example which can help you.
I hope this helps someone ;)