from django.core.management.base import BaseCommand from observations.models import Observation class Command(BaseCommand): help = "Complète le champ 'code' des observations qui n'en ont pas encore." def handle(self, *args, **options): qs = Observation.objects.filter(code__isnull=True).order_by('pk') count = qs.count() if not count: self.stdout.write(self.style.SUCCESS("Toutes les observations ont déjà un code.")) return updated = 0 for obs in qs: obs.code = f"OBS{obs.pk:05d}" updated += 1 Observation.objects.bulk_update(qs, ['code']) self.stdout.write(self.style.SUCCESS(f"{updated} observation(s) mises à jour."))