diff --git a/edx_exams/apps/core/management/commands/bulk_add_course_staff.py b/edx_exams/apps/core/management/commands/bulk_add_course_staff.py index 0d4178a3..34a55fc9 100644 --- a/edx_exams/apps/core/management/commands/bulk_add_course_staff.py +++ b/edx_exams/apps/core/management/commands/bulk_add_course_staff.py @@ -4,7 +4,7 @@ import time from django.core.management.base import BaseCommand -from django.db import transaction +from django.db import transaction, IntegrityError from edx_exams.apps.core.models import CourseStaffRole, User @@ -66,9 +66,13 @@ def add_course_staff_from_csv(self, csv_file, batch_size, batch_delay): users = {} for i in range(0, len(reader), batch_size): - users_dict = dict([(u.username, u) for (u, c) - in [User.objects.get_or_create(username=row.get('username'), email=row.get('email')) - for row in reader[i:i + batch_size]]]) + users_list = [] + for row in reader[i:i + batch_size]: + try: + users_list.append(User.objects.get_or_create(username=row.get('username'), email=row.get('email'))) + except IntegrityError: + continue + users_dict = dict([(u.username, u) for (u, c) in users_list]) users.update(users_dict) time.sleep(batch_delay)