I have this string variable
auxi_espec = '1, 3, 5, 7,'
And I need to convert it to array in order to make a queryset where I filter using __in. (Possibly I think I'll also need to slice the last comma).
You need to use split() function:
>>> auxi_espec = '1, 3, 5, 7,'
>>> auxi_espec_lst = [x.strip() for x in auxi_espec.split(',')][:-1]
>>> auxi_espec_lst
['1', '3', '5', '7']
If you want to parse those numbers into integer:
>>> auxi_espec = '1, 3, 5, 7,'
>>> auxi_espec_lst = [int(x.strip()) for x in auxi_espec.split(',') if x]
>>> auxi_espec_lst
[1, 3, 5, 7]
Django accepts a lot of iterables for in lookup, so if format of string you mentioned is set in stone this split is enough as list of strings will do.
ids = auxi_espec[0:-1].split(', ') # ['1', '3', '5', '7']
instances = MyModel.objects.filter(id__in=ids)
__in, are you referring to something on django?