# Generated by Django 3.1.3 on 2022-10-11 11:39 from django.db import migrations, models from django.db.models import Sum def fill_deductable_rest(apps, schema_editor): EcoAccount = apps.get_model("compensation", "EcoAccount") accs = EcoAccount.objects.all() for acc in accs: deductions = acc.deductions.filter( intervention__deleted=None, ) deductions_surfaces = deductions.aggregate(Sum("surface"))["surface__sum"] or 0 available_surfaces = acc.deductable_surface or deductions_surfaces rest = available_surfaces - deductions_surfaces acc.deductable_rest = rest acc.save() class Migration(migrations.Migration): dependencies = [ ('compensation', '0010_auto_20220815_1030'), ] operations = [ migrations.AddField( model_name='ecoaccount', name='deductable_rest', field=models.FloatField(blank=True, default=0, help_text='Amount of deductable rest', null=True), ), migrations.RunPython(fill_deductable_rest) ]