eugene
eugene

Reputation: 41665

Django, get list of an attribute for multiple attributes

id_list = Foo.objects.values_list('id', flat=True)
name_list = Foo.objects.values_list('name', flat=True)

Can I get the two list conveniently, efficiently?

Upvotes: 0

Views: 925

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599540

If by efficiently you mean in a single query, then you can get both attributes in one call and use zip to decompose them into separate lists:

values = Foo.objects.values_list('id', 'name')
id_list, name_list = zip(*values)

Upvotes: 1

Related Questions