from decimal import Decimal, ROUND_HALF_UP import django import os os.environ.setdefault("DJANGO_SETTINGS_MODULE", "excel_mimic.settings") django.setup() from sheets.models import ExcelEntry TWO_DEC = Decimal("0.00") def q2(x): return Decimal(str(x)).quantize(TWO_DEC, rounding=ROUND_HALF_UP) fields = ["korrig_druck", "nm3", "lhe", "lhe_ges"] to_update = [] total = 0 changed_count = 0 qs = ExcelEntry.objects.all().only("id", *fields) for e in qs.iterator(chunk_size=500): total += 1 changed = False for f in fields: val = getattr(e, f) if val is not None: new = q2(val) if new != val: setattr(e, f, new) changed = True if changed: to_update.append(e) changed_count += 1 if len(to_update) >= 500: ExcelEntry.objects.bulk_update(to_update, fields) to_update = [] print(f"processed={total}, changed={changed_count}") if to_update: ExcelEntry.objects.bulk_update(to_update, fields) print(f"DONE. processed={total}, changed={changed_count}")