Skip to content
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

Handle get_prefetch_queryset deprecation for Django 5.0+ #198

Closed
wants to merge 1 commit into from
Closed
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
25 changes: 23 additions & 2 deletions modelcluster/fields.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import unicode_literals

from django import VERSION as DJANGO_VERSION
from django.core import checks
from django.db import IntegrityError, connections, router
from django.db.models import CASCADE
Expand Down Expand Up @@ -84,7 +85,10 @@ def _apply_rel_filters(self, queryset):
# to work correctly with prefetch
return queryset._next_is_sticky().all()

def get_prefetch_queryset(self, instances, queryset=None):
def get_prefetch_querysets(self, instances, querysets=None):
if querysets and len(querysets) != 1:
raise ValueError("get_prefetch_querysets() must be passed exactly one queryset")
queryset = querysets[0] if querysets else None
if queryset is None:
db = self._db or router.db_for_read(self.model, instance=instances[0])
queryset = super().get_queryset().using(db)
Expand All @@ -103,6 +107,13 @@ def get_prefetch_queryset(self, instances, queryset=None):
cache_name = rel_field.related_query_name()
return qs, rel_obj_attr, instance_attr, False, cache_name, False

# Remove once we only support Django 5.0+
if not DJANGO_VERSION >= (5, 0):
def get_prefetch_queryset(self, instances, queryset=None):
if queryset is None:
return self.get_prefetch_querysets(instances)
return self.get_prefetch_querysets(instances, querysets=[queryset])

def get_object_list(self):
"""
return the mutable list that forms the current in-memory state of
Expand Down Expand Up @@ -338,8 +349,11 @@ def get_queryset(self):

return FakeQuerySet(rel_model, results)

def get_prefetch_queryset(self, instances, queryset=None):
def get_prefetch_querysets(self, instances, querysets=None):
# Derived from Django's ManyRelatedManager.get_prefetch_queryset.
if querysets and len(querysets) != 1:
raise ValueError("get_prefetch_querysets() must be passed exactly one queryset")
queryset = querysets[0] if querysets else None
if queryset is None:
queryset = super().get_queryset()

Expand Down Expand Up @@ -374,6 +388,13 @@ def get_prefetch_queryset(self, instances, queryset=None):
False,
)

# Remove once we only support Django 5.0+
if not DJANGO_VERSION >= (5, 0):
def get_prefetch_queryset(self, instances, queryset=None):
if queryset is None:
return self.get_prefetch_querysets(instances)
return self.get_prefetch_querysets(instances, querysets=[queryset])

def _apply_rel_filters(self, queryset):
# Required for get_prefetch_queryset.
return queryset._next_is_sticky()
Expand Down
Loading