I have a list:
destinations = test_task.destination_list() – which is a list of strings, containing a countries names assigned to current test_task object.
And i have a table CDR with fields:
id(pk)
number(charfield)
area(charfield).
I need to select 3 random numbers from CDR table where area=destination for each destination.
Can i do it using only one select?
Or do i need to make a loop for each destination?
Which is a right way?
as I mentioned in the comments I think there is no viable ways to do that with a single query.
If you use a loop you can combine random ordering, values_list and “in” operator for querysets.
be aware that this query may be slow as documented in the *order_by* docs, and may become even more slow if the number of destinations is high.