Skip to content

Update to Django 6.0 #280

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions .evergreen/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
set -eux

# Install django-mongodb-backend
/opt/python/3.10/bin/python3 -m venv venv
/opt/python/3.12/bin/python3 -m venv venv
. venv/bin/activate
python -m pip install -U pip
pip install -e .

# Install django and test dependencies
git clone --branch mongodb-5.2.x https://github.com/mongodb-forks/django django_repo
git clone --branch mongodb-6.0.x https://github.com/mongodb-forks/django django_repo
pushd django_repo/tests/
pip install -e ..
pip install -r requirements/py3.txt
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/linters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
persist-credentials: false
- uses: actions/setup-python@v5
with:
python-version: '3.10'
python-version: '3.12'
cache: 'pip'
cache-dependency-path: 'pyproject.toml'
- name: Install Python dependencies
Expand All @@ -37,7 +37,7 @@ jobs:
with:
cache: 'pip'
cache-dependency-path: 'pyproject.toml'
python-version: '3.10'
python-version: '3.12'
- name: Install dependencies
run: |
pip install -U pip
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test-python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
uses: actions/checkout@v4
with:
repository: 'mongodb-forks/django'
ref: 'mongodb-5.2.x'
ref: 'mongodb-6.0.x'
path: 'django_repo'
persist-credentials: false
- name: Install system packages for Django's Python test dependencies
Expand Down
4 changes: 2 additions & 2 deletions .readthedocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ python:
- docs

build:
os: ubuntu-22.04
os: ubuntu-24.04
tools:
python: "3.11"
python: "3.12"
2 changes: 1 addition & 1 deletion django_mongodb_backend/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "5.2.0b0.dev0"
__version__ = "6.0.0b0.dev0"

# Check Django compatibility before other imports which may fail if the
# wrong version of Django is installed.
Expand Down
29 changes: 26 additions & 3 deletions django_mongodb_backend/aggregates.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
from django.db.models.aggregates import Aggregate, Count, StdDev, Variance
from django.db.models.expressions import Case, Value, When
from django.db.models.aggregates import (
Aggregate,
AggregateFilter,
Count,
StdDev,
StringAgg,
Variance,
)
from django.db.models.expressions import Case, Col, Value, When
from django.db.models.lookups import IsNull

from .query_utils import process_lhs
Expand All @@ -16,7 +23,11 @@ def aggregate(
resolve_inner_expression=False,
**extra_context, # noqa: ARG001
):
if self.filter:
# TODO: isinstance(self.filter, Col) works around failure of
# aggregation.tests.AggregateTestCase.test_distinct_on_aggregate. Is this
# correct?
if self.filter is not None and not isinstance(self.filter, Col):
# Generate a CASE statement for this aggregate.
node = self.copy()
node.filter = None
source_expressions = node.get_source_expressions()
Expand All @@ -31,6 +42,10 @@ def aggregate(
return {f"${operator}": lhs_mql}


def aggregate_filter(self, compiler, connection, **extra_context):
return self.condition.as_mql(compiler, connection, **extra_context)


def count(self, compiler, connection, resolve_inner_expression=False, **extra_context): # noqa: ARG001
"""
When resolve_inner_expression=True, return the MQL that resolves as a
Expand Down Expand Up @@ -72,8 +87,16 @@ def stddev_variance(self, compiler, connection, **extra_context):
return aggregate(self, compiler, connection, operator=operator, **extra_context)


def string_agg(self, compiler, connection, **extra_context): # # noqa: ARG001
from django.db import NotSupportedError

raise NotSupportedError("StringAgg is not supported.")


def register_aggregates():
Aggregate.as_mql = aggregate
AggregateFilter.as_mql = aggregate_filter
Count.as_mql = count
StdDev.as_mql = stddev_variance
StringAgg.as_mql = string_agg
Variance.as_mql = stddev_variance
4 changes: 2 additions & 2 deletions django_mongodb_backend/cache.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pickle
from datetime import datetime, timezone
from datetime import UTC, datetime

from django.core.cache.backends.base import DEFAULT_TIMEOUT, BaseCache
from django.core.cache.backends.db import Options
Expand Down Expand Up @@ -72,7 +72,7 @@ def get_backend_timeout(self, timeout=DEFAULT_TIMEOUT):
if timeout is None:
return datetime.max
timestamp = super().get_backend_timeout(timeout)
return datetime.fromtimestamp(timestamp, tz=timezone.utc)
return datetime.fromtimestamp(timestamp, tz=UTC)

def get(self, key, default=None, version=None):
return self.get_many([key], version).get(key, default)
Expand Down
11 changes: 11 additions & 0 deletions django_mongodb_backend/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ class DatabaseFeatures(BaseDatabaseFeatures):
# Value.as_mql() doesn't call output_field.get_db_prep_save():
# https://github.com/mongodb/django-mongodb-backend/issues/282
"model_fields.test_jsonfield.TestSaveLoad.test_bulk_update_custom_get_prep_value",
# This backend overrides DatabaseCreation.create_test_db() so the
# deprecation warnings stacklevel points to the wrong file.
"backends.base.test_creation.TestDbCreationTests.test_serialize_deprecation",
# This backend has a custom format_debug_sql().
"backends.tests.LastExecutedQueryTest.test_debug_sql",
# StringAgg is not supported.
"aggregation.tests.AggregateTestCase.test_distinct_on_stringagg",
"aggregation.tests.AggregateTestCase.test_string_agg_escapes_delimiter",
"aggregation.tests.AggregateTestCase.test_string_agg_filter",
"aggregation.tests.AggregateTestCase.test_string_agg_filter_in_subquery",
"aggregation.tests.AggregateTestCase.test_stringagg_default_value",
}
# $bitAnd, #bitOr, and $bitXor are new in MongoDB 6.3.
_django_test_expected_failures_bitwise = {
Expand Down
4 changes: 1 addition & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ dynamic = ["version", "dependencies"]
description = "Django MongoDB Backend"
readme = "README.md"
license = {file="LICENSE"}
requires-python = ">=3.10"
requires-python = ">=3.12"
authors = [
{ name = "The MongoDB Python Team" },
]
Expand All @@ -27,8 +27,6 @@ classifiers = [
"Operating System :: OS Independent",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
]
Expand Down