Skip to content

Commit c8869b8

Browse files
committed
change SQLCompiler.get_collection() to a collection property
1 parent e4fe62a commit c8869b8

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
@@ -409,7 +409,8 @@ def project_field(column):
409409
def collection_name(self):
410410
return self.query.get_meta().db_table
411411

412-
def get_collection(self):
412+
@cached_property
413+
def collection(self):
413414
return self.connection.get_collection(self.collection_name)
414415

415416
def get_lookup_pipeline(self):
@@ -516,8 +517,7 @@ def execute_sql(self, returning_fields=None):
516517
@wrap_database_errors
517518
def insert(self, docs, returning_fields=None):
518519
"""Store a list of documents using field columns as element names."""
519-
collection = self.get_collection()
520-
inserted_ids = collection.insert_many(docs).inserted_ids
520+
inserted_ids = self.collection.insert_many(docs).inserted_ids
521521
return inserted_ids if returning_fields else []
522522

523523

@@ -582,12 +582,11 @@ def update(self, values):
582582

583583
@wrap_database_errors
584584
def execute_update(self, update_spec):
585-
collection = self.get_collection()
586585
try:
587586
criteria = self.build_query().mongo_query
588587
except EmptyResultSet:
589588
return 0
590-
return collection.update_many(criteria, update_spec).matched_count
589+
return self.collection.update_many(criteria, update_spec).matched_count
591590

592591
def check_query(self):
593592
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)