Skip to content

Commit ee79566

Browse files
committed
changes
1 parent 762787f commit ee79566

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

.github/workflows/test.yml

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ jobs:
1414
runs-on: ubuntu-latest
1515
strategy:
1616
matrix:
17+
psycopg-package: ["psycopg2", "psycopg"]
1718
python-version: ["3.9", "3.10", "3.11"]
1819
django-version: ["4.0", "4.1", "4.2", "5.0", "5.1", "5.2"]
1920
exclude:
@@ -53,6 +54,7 @@ jobs:
5354
# Using -e .[dev] installs the current package in editable mode with dev extras
5455
uv pip install --system -e .[dev]
5556
uv pip install --system Django==${{ matrix.django-version }}
57+
uv pip install --system ${{ matrix.psycopg-package }}
5658
- name: Run tests
5759
run: |
5860
./check-lint.sh

django_querysets_single_query_fetch/service.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
get_related_populators,
2525
)
2626
from django.utils.dateparse import parse_datetime
27-
from psycopg2.extensions import QuotedString
2827

2928
logger = logging.getLogger(__name__)
3029

@@ -189,6 +188,19 @@ def _get_compiler_from_queryset(self, queryset: QuerysetWrapperType) -> Any:
189188

190189
return compiler
191190

191+
def _get_sanitized_sql_param(self, param: str) -> str:
192+
try:
193+
from psycopg import sql
194+
195+
return sql.quote(param)
196+
except ImportError:
197+
try:
198+
from psycopg2.extensions import QuotedString
199+
200+
return QuotedString(param).getquoted().decode("utf-8")
201+
except ImportError:
202+
raise ImportError("psycopg or psycopg2 not installed")
203+
192204
def _get_django_sql_for_queryset(self, queryset: QuerysetWrapperType) -> str:
193205
"""
194206
gets the sql that django would normally execute for the queryset, return empty string
@@ -212,7 +224,7 @@ def _get_django_sql_for_queryset(self, queryset: QuerysetWrapperType) -> str:
212224
for param in params:
213225
if isinstance(param, str):
214226
# this is to handle special char handling
215-
param = QuotedString(param).getquoted().decode("utf-8")
227+
param = self._get_sanitized_sql_param(param)
216228
elif isinstance(param, UUID) or isinstance(param, datetime.datetime):
217229
param = f"'{param}'"
218230
elif isinstance(param, int) or isinstance(param, float):
@@ -419,7 +431,7 @@ def execute(self) -> list[list[Any]]:
419431
raw_sql = f"""
420432
SELECT
421433
json_build_object(
422-
{', '.join([f"'{i}', {sql}" for i, sql in enumerate(non_empty_django_sqls_for_querysets)])}
434+
{", ".join([f"'{i}', {sql}" for i, sql in enumerate(non_empty_django_sqls_for_querysets)])}
423435
)
424436
"""
425437
with connections["default"].cursor() as cursor:

pyproject.toml

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ classifiers = [
3131
]
3232
dependencies = [
3333
"django>=4.0,<5.3", # Updated to include Django 5.2
34-
"psycopg2==2.9.9" # Keep specific version for now
3534
]
3635

3736
[project.urls]

0 commit comments

Comments
 (0)