from django.core.management.base import BaseCommand from django.db.models import F from apps.complaints.models import Complaint, ComplaintInvolvedDepartment from apps.observations.models import Observation from apps.complaints.models import Inquiry class Command(BaseCommand): help = "Backfill sent_to_department=True on all records that have a department assigned" def handle(self, *args, **options): c_updated = Complaint.objects.filter( department__isnull=False, sent_to_department=False ).update( sent_to_department=True, sent_to_department_at=F("created_at"), ) self.stdout.write(f"Complaints updated: {c_updated}") o_updated = Observation.objects.filter( assigned_department__isnull=False, sent_to_department=False ).update( sent_to_department=True, sent_to_department_at=F("created_at"), ) self.stdout.write(f"Observations updated: {o_updated}") i_updated = Inquiry.objects.filter( department__isnull=False, sent_to_department=False ).update( sent_to_department=True, sent_to_department_at=F("created_at"), ) self.stdout.write(f"Inquiries updated: {i_updated}") cid_updated = ComplaintInvolvedDepartment.objects.filter( sent=False, complaint__department__isnull=False ).update(sent=True) self.stdout.write(f"Involved departments updated: {cid_updated}") self.stdout.write(self.style.SUCCESS("Done"))