Skip to content

Commit 0ded0df

Browse files
timgrahamWaVEV
authored andcommitted
change SQLCompiler.get_collection() to a collection property
1 parent 1698963 commit 0ded0df

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
lines changed

django_mongodb/compiler.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,8 @@ def project_field(column):
407407
def collection_name(self):
408408
return self.query.get_meta().db_table
409409

410-
def get_collection(self):
410+
@cached_property
411+
def collection(self):
411412
return self.connection.get_collection(self.collection_name)
412413

413414
def get_lookup_pipeline(self):
@@ -513,8 +514,7 @@ def execute_sql(self, returning_fields=None):
513514
@wrap_database_errors
514515
def insert(self, docs, returning_fields=None):
515516
"""Store a list of documents using field columns as element names."""
516-
collection = self.get_collection()
517-
inserted_ids = collection.insert_many(docs).inserted_ids
517+
inserted_ids = self.collection.insert_many(docs).inserted_ids
518518
return inserted_ids if returning_fields else []
519519

520520

@@ -579,12 +579,11 @@ def update(self, values):
579579

580580
@wrap_database_errors
581581
def execute_update(self, update_spec):
582-
collection = self.get_collection()
583582
try:
584583
criteria = self.build_query().mongo_query
585584
except EmptyResultSet:
586585
return 0
587-
return collection.update_many(criteria, update_spec).matched_count
586+
return self.collection.update_many(criteria, update_spec).matched_count
588587

589588
def check_query(self):
590589
super().check_query()

django_mongodb/query.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def __init__(self, compiler):
4343
self.query = compiler.query
4444
self._negated = False
4545
self.ordering = []
46-
self.collection = self.compiler.get_collection()
46+
self.collection = self.compiler.collection
4747
self.collection_name = self.compiler.collection_name
4848
self.mongo_query = getattr(compiler.query, "raw_query", {})
4949
self.subquery = None

0 commit comments

Comments
 (0)