Přejít na obsah
Menu
You need to be registered to interact with the community.
This question has been flagged
4 Odpovědi
10437 Zobrazení

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
Zrušit
Nejlepší odpověď

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
Zrušit
Autor

it works :) thanks

Nejlepší odpověď

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
Zrušit
Autor Nejlepší odpověď

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

Avatar
Zrušit

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

Autor

it works thank you :)

Nejlepší odpověď

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
Zrušit
Related Posts Odpovědi Zobrazení Aktivita
4
pro 23
22294
5
čvc 24
15659
1
čvn 22
27179
9
kvě 22
53431
0
čvc 20
2982