These lines of code should apply changes made by users to the same field ‘score’ in several objects:
if form.is_valid():
for key,value in form.cleaned_data.items():
an_id = int(key)
a_new_score = float(value)
obj = qs.filter(struct_id=an_id).get()
print('new score for',an_id,'will be:',a_new_score)
#1 obj.score=new_score
#2 print('score for',an_id,'was _not_ changed to', obj.score,'?!')
obj.update(score=a_new_score)
qs.bulk_update(list(qs),['score'])
where qs is a queryset of those several objects to be changed.
The code runs and does the expected, but it queries the database at every turn of the loop,
and the final bulk_update is superfluous and may (should!) be omitted.
On the other side, if I uncomment #1 then nothing gets done, as #2 would show if executed.
So I have 2 questions: 1) how should I rewrite commented line #1 so that no queries are done in the loop, and 2) what is actually achieved by my present line #1, that gets executed without errors?
Thank you!