How to get a clean list from the query method values\Django values_list?

The question seems simple, but I haven't found a solution after a long googling and samples. But the task is simple: I want to get a clean list of IDs (not a queryset, and any other impurities, for example in this query:
Mode.objects.filter(somefield__in=[1,2,3]).values_list('id', flat=True)
What I get are objects of qs in the list:
[<queryset [6]>, <queryset [3]>, <queryset [4]>, <queryset [12]>]</queryset></queryset></queryset></queryset>
even if I'm putting it in list using list().

And I would like to something like: [45, 123, 34, 98], i.e. a single list. Tell me how it's done? I solved this problem at the moment - 1) make list 2) translation of the list into a string (str), 2) and then vypilivaya from the string of all digits using regex. Rough, but it works. But is there no simple method that will work how I want?
June 14th 19 at 18:24
2 answers
June 14th 19 at 18:26
ids = list(Mode.objects.filter(somefield__in=[1,2,3]).values_list('id', flat=True))
Here is the full code:
# Select all objects load for the date
loads = Load.objects.filter(date=datetime.now())
# the for loop loads in each iteration even cycle with obtaining values for rent_id
[i.values_list('rent_id', flat=True) \
for i in [load.loadpara_set.all() for load in loads] if i]
rent_id is a FK to another model, it won't change anything. There is nothing non-standard. Django Version 1.11. I understand why he gives a list from a queryset, but how to turn it into a list of net id? Your code does not work, mentioned in the question about it. - Candida93 commented on June 14th 19 at 18:29
The code works 100%. What you wrote is now a lot different, from the original question. It is logical that you have acquired a list of querysets. Why cause a bunch of queries in a loop? Write one request.

LoadPara.filter(load__date=datetime.now()).values_list('rent_id', flat=True) - Willow17 commented on June 14th 19 at 18:32
so did the way it works. Duplicate here what I wrote to the person below: what to do if I have such LoadPara 6 pieces. Ie I have 6 models (in the idea they are all connected with the main Load model). It turns out I have to go on the reverse: don't get all the Load at the specified date, and to appeal directly to the LoadXXX and get the data I'm interested in (in this case ID rent_id). I have a lot of such situations and I have to first do this:
loads =load.objects.filter(data=...)
for load in loads:
a = load.loadaaa_set.all()
b = load.loadbbb_set.all()
c = load.loadccc_set.all()
aa = [i for i in a] # here looking for one
bb = [i for i in b] # here are looking for second
cc = [i for i in c] # here trestle

and then begins the recovery nuzhnyy data from each set. But you, as I write, it makes a lot of queries in a loop. I do want to ask a separate question on this topic, because often we have to do. This is mainly used for validation, because I need to check something in all instances. In this, I'm doing loops, and generally all of these procedures. - Candida93 commented on June 14th 19 at 18:35
June 14th 19 at 18:28
I can assume that instead:

# Select all objects load for the date
loads = Load.objects.filter(date=datetime.now())
# the for loop loads in each iteration even cycle with obtaining values for rent_id
[i.values_list('rent_id', flat=True) \
for i in [load.loadpara_set.all() for load in loads] if i]


So you need:

list(LoadPara.objects.filter(load__date=datetime.now().date()).values_list('rent_id', flat=True))
thank you! I will try and I will unsubscribe about results. - Candida93 commented on June 14th 19 at 18:31
I tried, so I wanted to list.
What should I do if I have such LoadPara 6 pieces. Ie I have 6 model (in the idea they are all connected with the main Load model). It turns out I have to go on the reverse: don't get all the Load at the specified date, and to appeal directly to the LoadXXX and get interested in me data. - Willow17 commented on June 14th 19 at 18:34
Can also be done with one query and in a loop to group lists - Candida93 commented on June 14th 19 at 18:37
want to create another question in which you clearly state what you are trying to receive and what difficulties - Candida93 commented on June 14th 19 at 18:40

Find more questions by tags Django