Skip to content

Commit

Permalink
temp: show different approaches
Browse files Browse the repository at this point in the history
  • Loading branch information
varshamenon4 committed Aug 27, 2024
1 parent 32ddf27 commit 49a0b87
Showing 1 changed file with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,17 @@ def handle(self, *args, **options):

logger.info('Bulk add course staff complete!')

# todo: commented this out in the hopes it is not needed and would autocommit changes
# @transaction.atomic
def add_course_staff_from_csv(self, csv_file, batch_size, batch_delay):
"""
Add the given set of course staff provided in csv
"""
reader = list(csv.DictReader(csv_file))

# todo: remove as needed. This is the new way I'm suggesting after research. I have added the batch size
# (remove batch delay if going this route) and I've used get_or_create instead since the db call is required
# anyway.
User.objects.bulk_create(
(User(
username=row.get('username'),
Expand All @@ -83,8 +87,9 @@ def add_course_staff_from_csv(self, csv_file, batch_size, batch_delay):
batch_size=batch_size,
)

users = {}
# users = {}

# todo: remove as needed. This is the original way, but this requires a db call.
# bulk create users
# for i in range(0, len(reader), batch_size):
# User.objects.bulk_create(
Expand All @@ -95,6 +100,9 @@ def add_course_staff_from_csv(self, csv_file, batch_size, batch_delay):
# ignore_conflicts=True,
# )

# todo: remove as needed, this is the way I tried to get bulk create users return value and add to dictionary
# to avoid the db call. Unfortunately this doesn't work as the return value doesn't include the PK for auto
# fields
# bulk_users = User.objects.bulk_create(
# (User(
# username=row.get('username'),
Expand Down

0 comments on commit 49a0b87

Please sign in to comment.