0
votes

I have some selection field in my model. Here example:

class MyModel(models.Model):
    _name = 'my_app.my_model'

    example_selection = fields.Selection(
        [
             ('first', 'First'),
             ('second', 'Second'),
             # etc.
        ], 
        string='My selection', 
    )

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.

enter image description here

Thanks in advance.

3

3 Answers

1
votes

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):

odoo.define('your_module.form_widgets', function (require) {
"use strict";

var core = require('web.core');
var FieldSelection = core.form_widget_registry.get('selection');

var MySelection = FieldSelection.extend({
    // add events to base events of FieldSelection
    events: _.defaults({
        // we will change of visibility on focus of field
        'focus select': 'onFocus'
    }, FieldSelection.prototype.events),
    onFocus: function() {
      if (
          // check values of fields. for example I need to check many fields
          this.field_manager.fields.name_field_1.get_value() == 'value1' &&
          this.field_manager.fields.name_field_2.get_value() == 'value2' /* && etc fields...*/   
      ) {
          // for example just hide all options. You can create any kind of logic here
          this.$el.find('option').hide();
      } 
    }
});

// register your widget
core.form_widget_registry.add('your_selection', MySelection);
});

After this you need just set your widget to field in your view like this:

<field name="example_selection" widget="your_selection"/>

If you don't know how to include static of your module HERE example which can help you.

I hope this helps someone ;)

1
votes

It's a bit too late. In my case, I did it like this :

odoo.define('my_module.custom_selection', function(require) {
"use strict";

var registry = require('web.field_registry');
var relational_fields = require('web.relational_fields');

var MySelection = relational_fields.FieldRadio.extend({
    init: function() {
        this._super.apply(this, arguments);
        // use to decrement in splice, bc position change when element is removed
        let decrement = 0;
        // this.values can be undefined or [[], [], []]
        // copying the content of original array or []
        let value_copies = this.values? [...this.values]: [];
        
        for (let index = 0; index < value_copies.length; index++) {
            // 'other' is the value to be removed
            if (value_copies[index].includes('other')) {
                this.values.splice(index - decrement, 1);
                decrement++;
            }
        }
    },
});

registry.add('custom_selection', MySelection);

return MySelection;
});

You can check my repo here: https://github.com/m0r7y/wdgt_hide_option

0
votes

AFAIK this is not possible, but you can achieve something similar if you use a MAny2one instead of a selection (thus using the domain) end in the view you can use

<field name="example_with_domain" widget="selection"/>

to obtain the same visual behaviour (no create, no edit) of a selection field.