import os import django os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings') django.setup() from core.models import Voter, EventParticipation, ParticipationStatus def update_voters(): # 1. Identify all households where at least one person has 'Attended' an event attended_status = ParticipationStatus.objects.filter(name='Attended') if not attended_status.exists(): print("No 'Attended' participation status found.") return # Find voters who attended voter_ids_who_attended = EventParticipation.objects.filter( participation_status__in=attended_status ).values_list('voter_id', flat=True) # Get households of those voters households_who_attended = Voter.objects.filter( id__in=voter_ids_who_attended ).values('address_street', 'city', 'state', 'zip_code', 'tenant').distinct() print(f"Found {len(households_who_attended)} households with 'Attended' members.") total_updated = 0 for hh in households_who_attended: # Update all voters in the same household who haven't been visited updated_count = Voter.objects.filter( address_street=hh['address_street'], city=hh['city'], state=hh['state'], zip_code=hh['zip_code'], tenant=hh['tenant'], door_visit=False, target_door_visit=True ).update(target_door_visit=False) total_updated += updated_count print(f"Total voters updated (target_door_visit set to False): {total_updated}") if __name__ == '__main__': update_voters()