No problem.  The original loop will still need a modification, though, to work.  Let me go over the change with you first, so that we can agree on the approach before I do another commit:

This code outline would need to change:

    for start in range(0, total, batch_size):
        ...
        for user_id in user_ids[start:min(start + batch_size, total)]:
            ...

The reason is that `user_ids` now contains a query that will produce different results each iteration.  The query filters out users that have a foreign key to the Friendship table, and adding those foreign keys is precisely what we're doing in the loop.  This means each slice iteration is going to skip over `start` number of users.

A loop like this would work instead:

    from __future__ import division
    ...
    num_batches = (total // batch_size) + 1
    for _ in range(num_batches):
        ...
        for user_id in user_ids[:batch_size]:
            ...