2

I have a function that I'm using and I need to pass it a list of arguments, however, it gives an error when I try to pass the list because it can't hash.

The function in question here is the values() method on a Django Queryset, but I think this is a more general python question.

So I have:

values_list = ['arg1', 'arg2', 'arg3']
models.MyModel.objects.values(values_list).all()

This gives an error unhashable type: 'list' when i try to run the code. Assuming that I can't alter the values method, how can I add this list to the arguments list in the normal manner?

for arg in values_list:
  queryset = MyModel.objects.values(arg)
Davor Lucic
  • 28,970
  • 8
  • 66
  • 76
Jack
  • 41
  • 2

3 Answers3

8

you need to use list unpacking

models.MyModel.objects.values(*values_list).all()

(note the asterisk)

second
  • 28,029
  • 7
  • 75
  • 76
0

can you use a tuple instead of a list? a tuple is hashable.

bigblind
  • 12,539
  • 14
  • 68
  • 123
0

It makes sense to make values_list as a tuple instead of a list, but if you can't alter the values method, then list unpacking is the way to go.

Bhavana C
  • 69
  • 1