This is common question that we need to return Queryset in Django just to be a list. When I try ask to several friends, most of them show me bunch of iterating codes. For example :
1 2 3 4 5 6 7 | Insurance ============================ | ID | Client | … | ============================ | 1 | Yodiaditya | … | 2 | John | …. | 3 | Yodiaditya | …. |
1 | insurer_list = Insurance.object.filter(client="yodiaditya") |
This will produce with QuerySet results. Now, how to create list that contain only list of insurer ID ?
It simple if we really care about Django documentation values_list(field, flat=True) .
Yes, it just simply as :
1 | list_id = Insurance.object.filter(client="yodiaditya").values_list("id", flat=True) |
Now you will got results :
1 | [1, 3] |
Easy but somehow people missing this 🙂