1👍
Django does not cache the raw queryset, so when you loop over it a second time Django fetches the original objects from the database again.
The solution is to convert the raw queryset to a list.
objs = list(ObjsModel.objects.raw(sql_raw_query))
for obj in objs:
obj.id = base64.urlsafe_b64encode(str(obj.id))
Source:stackexchange.com