buxxxbaum
buxxxbaum

Reputation: 65

Django read data from list of QuerySets

I have the following list

[<QuerySet [{'number': 123}]>, <QuerySet []>, <QuerySet []>, <QuerySet [{'number': 21323}]>, <QuerySet []>]

I want to extract the numbers in an other list with a for loop and skip the empty QuerySets, but was not successful yet. Is there an easy way?

Tahnk you

Upvotes: 1

Views: 64

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476557

You can work with:

[q['number'] for qs in qs_list for q in qs]

where qs_list is the list of QuerySets.

That being said, often a list of QuerySets means you are doing something inefficient, since you will here make a query per QuerySet in the list. Often you can derive the values all in the same query.

Upvotes: 1

Related Questions