def update_many(self, records: list[dict[str, Any]], hashes: dict[str, str]) -> None:
if not records:
return
keys = [str(r[self._identity_field]) for r in records]
existing = {
getattr(inst, self._identity_field): inst
for inst in self._by_keys(set(keys))
}
to_update = []
for record in records:
key = str(record[self._identity_field])
instance = existing.get(key)
if instance is None:
continue
for field in self._sync_fields:
if field in record:
setattr(instance, field, record[field])
setattr(instance, _HASH_FIELD, hashes.get(key, ''))
to_update.append(instance)
if not to_update:
return
logger.info('Updating %d records in %s', len(to_update), self._model_label)
model = self._get_model()
fields = [*list(self._sync_fields), _HASH_FIELD]
for start in range(0, len(to_update), self._batch_size):
batch = to_update[start:start + self._batch_size]
model.objects.bulk_update(batch, fields)