-
-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
_bulk_create flag is now creating related objects as well (#206)
* Move query count to a test utilitary file * Bulk create now also creates related objects * Add docs on _bulk_create flag and its limitations * Fix broken url on the docs * Update changelog * Link PR in the changelog * Fix mypy erros by expliciting that both methods return None
- Loading branch information
Showing
6 changed files
with
133 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from django.db import connection | ||
|
||
|
||
class QueryCount: | ||
""" | ||
Keep track of db calls. | ||
Example: | ||
======== | ||
qc = QueryCount() | ||
with qc.start_count(): | ||
MyModel.objects.get(pk=1) | ||
MyModel.objects.create() | ||
qc.count # 2 | ||
""" | ||
|
||
def __init__(self): | ||
self.count = 0 | ||
|
||
def __call__(self, execute, sql, params, many, context): | ||
""" | ||
`django.db.connection.execute_wrapper` callback. | ||
https://docs.djangoproject.com/en/3.1/topics/db/instrumentation/ | ||
""" | ||
self.count += 1 | ||
execute(sql, params, many, context) | ||
|
||
def start_count(self): | ||
"""Reset query count to 0 and return context manager for wrapping db queries.""" | ||
self.count = 0 | ||
|
||
return connection.execute_wrapper(self) |