Alex Rivera | Logout

How to remove extra ',' in tuple in django(python)

Asked 2011-05-21T13:59:50.297
10

I have one problem in building a list of id using django. if i choose more than 1 id it ok but if i choose only one it will produce extra ',' in list.

testa = tuple([k['id'] for k in queryset.values('id')])
print testa

if in the queryset have exactly 1 id, it will display

(1234,)

other than that it ok

(1234,1244)

How can i remove extra ', in list'

Edit
Report

1 Answer

1

Yo say you want a list but de facto you are creating a tuple.

To get a list of ids take a look at values_list

>>> Entry.objects.values_list('id', flat=True).order_by('id')
[1, 2, 3, ...]
>>> Entry.objects.filter(id=1).values_list('id', flat=True)
[1]
answered 2011-05-21T14:10:48.447

Your Answer