Skip to content

change SQLCompiler.get_collection() to a collection property #109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions django_mongodb/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,8 @@ def project_field(column):
def collection_name(self):
return self.query.get_meta().db_table

def get_collection(self):
@cached_property
def collection(self):
return self.connection.get_collection(self.collection_name)

def get_lookup_pipeline(self):
Expand Down Expand Up @@ -516,8 +517,7 @@ def execute_sql(self, returning_fields=None):
@wrap_database_errors
def insert(self, docs, returning_fields=None):
"""Store a list of documents using field columns as element names."""
collection = self.get_collection()
inserted_ids = collection.insert_many(docs).inserted_ids
inserted_ids = self.collection.insert_many(docs).inserted_ids
return inserted_ids if returning_fields else []


Expand Down Expand Up @@ -582,12 +582,11 @@ def update(self, values):

@wrap_database_errors
def execute_update(self, update_spec):
collection = self.get_collection()
try:
criteria = self.build_query().mongo_query
except EmptyResultSet:
return 0
return collection.update_many(criteria, update_spec).matched_count
return self.collection.update_many(criteria, update_spec).matched_count

def check_query(self):
super().check_query()
Expand Down
2 changes: 1 addition & 1 deletion django_mongodb/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def __init__(self, compiler):
self.query = compiler.query
self._negated = False
self.ordering = []
self.collection = self.compiler.get_collection()
self.collection = self.compiler.collection
self.collection_name = self.compiler.collection_name
self.mongo_query = getattr(compiler.query, "raw_query", {})
self.subquery = None
Expand Down