Skip to Content
Menu
This question has been flagged
4 Replies
9107 Views

Hello,

I have a selection field :

def _my_selection(self, cr, uid, context=None):

    lst= [('5', '5'), ('6', '6'), ('7', '7'), ('8', '8')]

    return lst

_columns = {

'mysection': fields.selection(_my_selection,'Seat'),

}

 I need to remove an item from list by key, I test this code but it does not work:

def _my_new_selection(self, cr, uid, context=None):

    lst= [('5', '5'), ('6', '6'), ('7', '7'), ('8', '8')]

     lst=lst.remove('6')

    return lst

I want to see this result lst = [('5', '5'), ('7', '7'), ('8', '8')] 

thank you !

Avatar
Discard
Best Answer

Try this code:

def _my_new_selection(self, cr, uid, context=None):

lst= [('5', '5'), ('6', '6'), ('7', '7'), ('8', '8')]

try:

lst.remove([item for item in lst if item[0] == '6'][0])

except IndexError as e:

pass

return lst

Avatar
Discard
Author

it works :) thanks

Best Answer

Hi dear ones,

To remove options from an odoo 15 selection field, proceed as follows:

Example:

Basic model

class SurveyQuestion(models.Model):
​_name = 'survey.question'

​question_type = fields.Selection([
('text_box', 'Multiple Lines Text Box'),
('char_box', 'Single Line Text Box'),
('numerical_box', 'Numerical Value'),
('date', 'Date'),
('datetime', 'Datetime'),
('simple_choice', 'Multiple choice: only one answer'),
('multiple_choice', 'Multiple choice: multiple answers allowed'),
('matrix', 'Matrix')], string='Question Type',
compute='_compute_question_type', readonly=False, store=True)

*****************************************************************************

Inheritance model

class SurveyQuestionInherited(models.Model):
​_inherit = 'survey.question'

question_type = fields.Selection(selection='_get_new_question_type', string='Type de question', compute='_compute_question_type', readonly=False, store=True)

@api.model
def _get_new_question_type(self):
"""Cette methode permet de mettre à jour les types de question,
Dans le but de retirer les options 'multiple_choice' et 'matrix'
"""
selection = [
('text_box', 'Zone de texte à plusieurs lignes'),
('char_box', 'Zone de texte sur une seule ligne'),
('numerical_box', 'Valeur numérique'),
('date', 'Date'),
('datetime', 'Date et heure'),
('simple_choice', 'Choix multiple : une seule réponse')
]
return selection

After several unsuccessful attempts, this method worked for me.

I really hope it helps !

Avatar
Discard
Author Best Answer

thank you but i need to delete this by key '6' not by index number

Avatar
Discard

ok maybe you can try this code. Chek my edited answer.

Author

it works thank you :)

Best Answer

Hi adel,


you can use this code:


lst= [('5', '5'), ('6', '6'), ('7', '7'), ('8', '8')]

j=0

for i in lst:

if i[0]=='6':

del lst[j]

print lst

j=j+1

result: [('5', '5'), ('7', '7'), ('8', '8')]

Avatar
Discard
Related Posts Replies Views Activity
4
Dec 23
20434
5
Jul 24
13733
1
Jun 22
25200
9
May 22
50528
0
Jul 20
1878