You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

25 lines
985 B

from django.core.management.base import BaseCommand
from apps.course.models import Course
from apps.account.models import ProfessorUser
import random
class Command(BaseCommand):
help = 'Assign at least one professor to every course (if missing)'
def handle(self, *args, **options):
# Gather all active professors
professors = list(ProfessorUser.objects.filter(is_active=True))
if not professors:
self.stdout.write(self.style.ERROR('No active professors found.'))
return
updated = 0
for course in Course.objects.all():
if course.professors.exists():
continue # already has a professor
professor = random.choice(professors)
course.professors.add(professor)
updated += 1
self.stdout.write(f'Added professor {professor.id} to course {course.id}')
self.stdout.write(self.style.SUCCESS(f'Finished. Updated {updated} course(s).'))