Created
November 23, 2018 20:45
-
-
Save bulv1ne/44f2756ff3e185db0541880d5bc1d329 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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