Skip to content

Commit

Permalink
ditte
Browse files Browse the repository at this point in the history
  • Loading branch information
specialunderwear committed Nov 29, 2023
1 parent 6a434a3 commit 2a3233c
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions oscar_odin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,37 @@ class DatabaseContext(dict):
many_to_one_items = []
many_to_many_items = []
foreign_key_items = []


def in_bulk(self, id_list=None, *, field_names=('pk',)):
"""
Return a dictionary mapping each of the given IDs to the object with
that ID. If `id_list` isn't provided, evaluate the entire QuerySet.
"""
assert not self.query.is_sliced, \
"Cannot use 'limit' or 'offset' with in_bulk"

if id_list is not None:
if not id_list:
return {}

# als id_list helemaal geen list is, dan kan je gewoon de standaard in_bulk gebruiken

filter_key = '{}__in'.format(field_name)
batch_size = connections[self.db].features.max_query_params
id_list = tuple(id_list)
# If the database has a limit on the number of query parameters
# (e.g. SQLite), retrieve objects in batches if necessary.
if batch_size and batch_size < len(id_list):
qs = ()
for offset in range(0, len(id_list), batch_size):
batch = id_list[offset:offset + batch_size]
# we hoeven alleen maar de primary ket te hebben
# je moet hier een models.Q(key_1=value_1, key_2=value2) doen and dan | (or)
qs += tuple(self.filter(**{filter_key: batch}).order_by().values("pk"))
else:
qs = self.filter(**{filter_key: id_list}).order_by()
else:
qs = self._chain()

return {getattr(obj, field_name): obj for obj in qs}

0 comments on commit 2a3233c

Please sign in to comment.