[Answer]-Update and save records in a django.db.models.query.QuerySet object?

1👍

table.objects.all() is a django.query.QuerySet object. When you run x[0], you actually calls its __getitem__ method.

You can find its implementation at github.

Below is a simplified version. I only remove the safety check and slice code.

def __getitem__(self, k):
    """
    Retrieves an item or slice from the set of results.
    """
    if self._result_cache is not None:
        return self._result_cache[k]

    qs = self._clone()
    qs.query.set_limits(k, k + 1)
    return list(qs)[0]

You see, when the queryset is not cached, each time you run queryset[index], it triggers a new sql query and returns a new object.

Leave a comment