def link_many(self, file_objects: list[File], instance: models.Model) -> None:
if not file_objects:
return
if instance.pk is None:
message = 'Cannot link files to an unsaved model instance.'
raise FileLinkError(message)
content_type = ContentType.objects.get_for_model(instance)
for file_obj in file_objects:
file_obj.content_type = content_type
file_obj.object_id = instance.pk
file_obj.related_field = self.related_field
file_obj.is_active = True
file_obj.is_deleted = False
File.objects.bulk_update(
file_objects,
['content_type', 'object_id', 'related_field', 'is_active', 'is_deleted'],
)