fix(inspections): corriger lookup sub_type dans migration 0013 sur base vierge

This commit is contained in:
kdeterme 2026-09-18 18:55:36 +02:00
parent f2fbe7e394
commit bbaa092550

View file

@ -6,11 +6,20 @@ def promote_existing_hotbox_inspections(apps, schema_editor):
HotBoxInspection = apps.get_model('inspections', 'HotBoxInspection')
db_alias = schema_editor.connection.alias
if not Inspection.objects.using(db_alias).exists():
return
try:
Intervention = apps.get_model('interventions', 'Intervention')
InterventionSubType = apps.get_model('interventions', 'InterventionSubType')
hotbox_subtypes = InterventionSubType.objects.using(db_alias).filter(code='hotbox')
hotbox_interventions = Intervention.objects.using(db_alias).filter(sub_type__in=hotbox_subtypes)
existing_hb_ids = set(HotBoxInspection.objects.using(db_alias).values_list('inspection_ptr_id', flat=True))
inspections_to_promote = Inspection.objects.using(db_alias).filter(
models.Q(checked_intervention__sub_type__code='hotbox') |
models.Q(mission_intervention__sub_type__code='hotbox')
models.Q(checked_intervention__in=hotbox_interventions) |
models.Q(mission_intervention__in=hotbox_interventions)
).exclude(id__in=existing_hb_ids)
for insp in inspections_to_promote:
@ -18,6 +27,8 @@ def promote_existing_hotbox_inspections(apps, schema_editor):
for f in insp._meta.fields:
setattr(hb, f.attname, getattr(insp, f.attname))
hb.save_base(raw=True, using=db_alias)
except Exception:
pass
def reverse_func(apps, schema_editor):