acknolodgia
acknolodgia

Reputation: 217

I want to convert Python models list to Dictionary

This is the thing that I want to show...

taskNameList = Task.objects.all()

using angularJS, I want to to show this list to my template such as...

list.html

 <div ng-init="tasks = {{ taskNameList }} ">

I am sure this code isn't correct.

or should it be something like.

<div ng-init="tasks = [{{ taskNameList }}]">

I also tried this but it is not still showing up...

Upvotes: 1

Views: 213

Answers (1)

Leistungsabfall
Leistungsabfall

Reputation: 6488

If you want dictionaries in a list use .values():

taskNameList = Task.objects.values()

>>> print(taskNameList)
[{'id': 1, 'name': 'i am the first task', ...}, ...]

If you want tuples in a list use .values_list():

taskNameList = Task.objects.values_list()

>>> print(taskNameList)
[(1, 'i am the first task'), ...]

Upvotes: 2

Related Questions