Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How to remove item from selection

By
ahmed
on 12/8/15, 10:07 AM 415 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 !

0
Quentin THEURET
On 12/8/15, 11:34 AM

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

it works :) thanks

ahmed
on 12/8/15, 11:39 AM
0
Mirco Strizzi
On 12/8/15, 10:42 AM

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')]

0
ahmed
On 12/8/15, 10:57 AM

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

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

Metalluffico di Ottaviano Basso, Mirco Strizzi
on 12/8/15, 11:18 AM

it works thank you :)

ahmed
on 12/8/15, 11:34 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 12/8/15, 10:07 AM
Seen: 415 times
Last updated: 12/8/15, 11:39 AM