bulk_update: Do updates in bulk where applicable.

This commit is contained in:
Clara Dantas 2020-08-03 14:55:57 -03:00 committed by Tim Abbott
parent f9cbefac41
commit f26d0affef
2 changed files with 4 additions and 4 deletions

View File

@ -90,14 +90,14 @@ def bulk_set_users_or_streams_recipient_fields(model: Model,
objects_dict = {obj.id: obj for obj in objects}
objects_to_update = set()
for recipient in recipients:
assert recipient.type == recipient_type
result = objects_dict.get(recipient.type_id)
if result is not None:
result.recipient = recipient
# TODO: Django 2.2 has a bulk_update method, so once we manage to migrate to that version,
# we take adventage of this, instead of calling save individually.
result.save(update_fields=['recipient'])
objects_to_update.add(result)
model.objects.bulk_update(objects_to_update, ['recipient'])
# This is only sed in populate_db, so doesn't really need tests
def bulk_create_streams(realm: Realm,

View File

@ -65,7 +65,7 @@ Usage: ./manage.py deliver_scheduled_messages
with transaction.atomic():
do_send_messages([self.construct_message(message)])
message.delivered = True
message.save(update_fields=['delivered'])
Message.objects.bulk_update(messages_to_deliver, ['delivered'])
cur_time = timezone_now()
time_next_min = (cur_time + timedelta(minutes=1)).replace(second=0, microsecond=0)