Reputation: 731
I have following JSON
which contain employee data.
all_emp = {"success":true,"data":{"users":
[[{"employeeId":"6","firstName":"Abhishek","lastName":"Amit"},
.
.
.
{"employeeId":"CT045","firstName":"Daniel","lastName":"Swamy"}
]]}}
I have two table Ideas
and Likes
.
I want to display name of peoples who liked that idea.
In likes table I have stored user_id
(who liked idea) and idea_id
is a foreign key
I want to replace user_id
of like table to name of that user using JSON
Following is my code in view file..
latest_ideas_list = Ideas.objects.order_by('-date_added')
for i in latest_ideas_list:
people_like = Likes.objects.values_list('user_id', flat=True).filter(idea_id=i.idea_id)
for person in people_like:
if any(d["employeeId"] == person for d in all_emp['data']['users']):
person = d['firstName'] + ' '+ d['lastName']
i.likelist = people_like
Following is template..
{% for person in anidea.likelist %}
<span>{{person}}</span>
{% endfor %}
I am getting error "list indices must be integers, not str"
I am new to python and django. Need help please.
Upvotes: 0
Views: 55
Reputation: 731
I resolve my issue with following code.
p_result = []
for person in people_like:
for d in all_emp['data']['users'][0]:
if str(person) in d["employeeId"]:
person = d["firstName"] + ' '+ d["lastName"]
p_result.append(person)
i.likelist = p_result
Upvotes: 0
Reputation: 1044
First of all here I guess all_emp['data'].['users']
must be all_emp['data']['users']
and d["employeeId"] == person for d in all_emp['data']['users']
here d is a list with only one element and that element then have all employer data.
So as a quick fix you can try this:
d["employeeId"] == person for d in all_emp['data']['users'][0]
Upvotes: 1