forked from mysociety/local-intelligence-hub
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8d38b82
commit 5481224
Showing
5 changed files
with
118 additions
and
76 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import psycopg | ||
from django.db import connection | ||
from django.db.models.query import QuerySet | ||
|
||
async def async_query(queryset: QuerySet): | ||
# Find and quote a database table name for a Model with users. | ||
# table_name = connection.ops.quote_name(GenericData._meta.db_table) | ||
# Create a new async connection. | ||
aconnection = await psycopg.AsyncConnection.connect( | ||
**{ | ||
**connection.get_connection_params(), | ||
"cursor_factory": psycopg.AsyncCursor, | ||
}, | ||
) | ||
async with aconnection: | ||
# Create a new async cursor and execute a query. | ||
async with aconnection.cursor() as cursor: | ||
await cursor.execute( | ||
str(queryset.query) | ||
) | ||
results = await cursor.fetchall() | ||
return [queryset.model(*result) for result in results] |
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