Skip to content

Commit

Permalink
fetch database converters once per query rather than for each result
Browse files Browse the repository at this point in the history
  • Loading branch information
timgraham committed May 3, 2024
1 parent 66f08ec commit 6001751
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions django_mongodb/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,23 @@ def results_iter(
except EmptyResultSet:
results = []

converters = self.get_converters(columns)
for entity in results:
yield self._make_result(entity, columns, tuple_expected=tuple_expected)
yield self._make_result(entity, columns, converters, tuple_expected=tuple_expected)

def has_results(self):
return bool(self.get_count(check_exists=True))

def _make_result(self, entity, columns, tuple_expected=False):
def get_converters(self, columns):
converters = {}
for column in columns:
backend_converters = self.connection.ops.get_db_converters(column)
field_converters = column.field.get_db_converters(self.connection)
if backend_converters or field_converters:
converters[column.target.column] = backend_converters + field_converters
return converters

def _make_result(self, entity, columns, converters, tuple_expected=False):
"""
Decode values for the given fields from the database entity.
Expand All @@ -73,12 +83,9 @@ def _make_result(self, entity, columns, tuple_expected=False):
value = entity.get(column, NOT_PROVIDED)
if value is NOT_PROVIDED:
value = field.get_default()
else:
elif converters:
# Decode values using Django's database converters API.
converters = self.connection.ops.get_db_converters(col) + field.get_db_converters(
self.connection
)
for converter in converters:
for converter in converters.get(column, ()):
value = converter(value, col, self.connection)
result.append(value)
if tuple_expected:
Expand Down

0 comments on commit 6001751

Please sign in to comment.