Allen Jun Jie
Allen Jun Jie

Reputation: 1

Django values function rename key of dict

In my databases design

id | key | value
 1 | ABC | 123

and what i trying to do

data.objects.filter(id__in=list).values('key', 'value')

and it return

[{'key': 'ABC', 'value': '123'}, ... ]

but instead of that, i want to do

[{'ABC': '123', ... ]

Noted: The result list will be 800k rows, because of the performance issues, i try to avoid using loop and want to use django feature to make it like this.

I will make sure the value from 'key' is unique.

Upvotes: 0

Views: 120

Answers (1)

Iain Shelvington
Iain Shelvington

Reputation: 32274

dict(data.objects.filter(id__in=list).values_list('key', 'value'))

Note, if you have duplicate values for key then you will only get the last value for that key

Upvotes: 1

Related Questions