Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy/how does iterating over a list and calling 'pass' each time fix this function?
    primarykey
    data
    text
    <p>I have written the following function: </p> <pre><code>def auto_update_ratings(amounts, assessment_entries_qs, lowest_rating=-1): start = 0 rating = lowest_rating ids = assessment_entries_qs.values_list('id', flat=True) for i in ids: # I have absolutely no idea why this seems to be required: pass # without this loop, the last AssessmentEntries fail to update # in the following for loop. for amount in amounts: end_mark = start + amount entries = ids[start:end_mark] a = assessment_entries_qs.filter(id__in=entries).update(rating=rating) start = end_mark rating += 1 </code></pre> <p>It does what it is supposed to do (i.e. update the relevant number of entries in <code>assessment_entries_qs</code> with each rating (starting at <code>lowest_rating</code>) as specified in <code>amounts</code>). Here is a simple example: </p> <pre><code>&gt;&gt;&gt; assessment_entries = AssessmentEntry.objects.all() &gt;&gt;&gt; print [ae.rating for ae in assessment_entries] [None, None, None, None, None, None, None, None, None, None] &gt;&gt;&gt; &gt;&gt;&gt; auto_update_ratings((2,4,3,1), assessment_entries, 1) &gt;&gt;&gt; print [ae.rating for ae in assessment_entries] [1, 1, 2, 2, 2, 2, 3, 3, 3, 4] </code></pre> <p>However, if I do not iterate through <code>ids</code> before iterating through <code>amounts</code>, the function only updates a subset of the queryset: with my current test data (approximately 250 <code>AssessmentEntries</code> in the queryset), it always results in exactly 84 <code>AssessmentEntries</code> not being updated. </p> <p>Interestingly, it is always the last iteration of the second for loop that does not result in any updates (although the rest of the code in that iteration does execute properly), <em>as well as a portion of the previous iteration</em>. The querysets are ordered_by('?') prior to being passed to this function, and the intended results are achieved if I simply add the previous 'empty' for loop, so it does not appear to be an issue with my data).</p> <p>A few more details, just in case they prove to be relevant:</p> <ul> <li><code>AssessmentEntry.rating</code> is a standard <code>IntegerField(null=True,blank=True)</code>.</li> <li>I am using this function purely for testing purposes, so I have only been executing it from iPython.</li> <li>Test database is SQLite.</li> </ul> <p><strong>Question:</strong> Can someone please explain why I appear to need to iterate through <code>ids</code>, despite not actually touching the data in any way, and why without doing so the function still (sort of) executes correctly, but always fails to update the last few items in the queryset despite apparently still iterating through them?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload