Skip to content

Instantly share code, notes, and snippets.

@bulv1ne
Created November 23, 2018 20:45
Show Gist options
  • Save bulv1ne/44f2756ff3e185db0541880d5bc1d329 to your computer and use it in GitHub Desktop.
Save bulv1ne/44f2756ff3e185db0541880d5bc1d329 to your computer and use it in GitHub Desktop.
from typing import List
from .models import Customer
def bulk_fetch_customer(ids:List[int]) -> List[Customer]:
hash_table = {
instance.pk: instance
for instance in ModelClass.objects.filter(id__in=ids)
}
instances = []
not_found = []
for id in ids:
try:
instances.append(hash_table[id])
except Customer.DoesNotExist:
not_found.append(id)
if not_found:
raise Customer.DoesNotExist("Customers with the following id don't exist: " + str(not_found))
return instances
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment