django-modelcluster 6.4__tar.gz → 6.5__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {django-modelcluster-6.4 → django_modelcluster-6.5}/CHANGELOG.txt +16 -0
- {django-modelcluster-6.4/django_modelcluster.egg-info → django_modelcluster-6.5}/PKG-INFO +19 -16
- {django-modelcluster-6.4 → django_modelcluster-6.5}/README.rst +3 -3
- {django-modelcluster-6.4 → django_modelcluster-6.5/django_modelcluster.egg-info}/PKG-INFO +19 -16
- {django-modelcluster-6.4 → django_modelcluster-6.5}/django_modelcluster.egg-info/SOURCES.txt +1 -2
- {django-modelcluster-6.4 → django_modelcluster-6.5}/modelcluster/contrib/taggit.py +16 -6
- {django-modelcluster-6.4 → django_modelcluster-6.5}/modelcluster/datetime_utils.py +1 -3
- {django-modelcluster-6.4 → django_modelcluster-6.5}/modelcluster/fields.py +81 -26
- {django-modelcluster-6.4 → django_modelcluster-6.5}/modelcluster/forms.py +171 -88
- {django-modelcluster-6.4 → django_modelcluster-6.5}/modelcluster/models.py +78 -34
- {django-modelcluster-6.4 → django_modelcluster-6.5}/modelcluster/queryset.py +95 -42
- django_modelcluster-6.5/modelcluster/tags.py +9 -0
- {django-modelcluster-6.4 → django_modelcluster-6.5}/modelcluster/utils.py +25 -14
- django_modelcluster-6.5/pyproject.toml +36 -0
- django_modelcluster-6.5/setup.cfg +4 -0
- django-modelcluster-6.4/modelcluster/tags.py +0 -8
- django-modelcluster-6.4/setup.cfg +0 -10
- django-modelcluster-6.4/setup.py +0 -41
- {django-modelcluster-6.4 → django_modelcluster-6.5}/LICENSE +0 -0
- {django-modelcluster-6.4 → django_modelcluster-6.5}/MANIFEST.in +0 -0
- {django-modelcluster-6.4 → django_modelcluster-6.5}/django_modelcluster.egg-info/dependency_links.txt +0 -0
- {django-modelcluster-6.4 → django_modelcluster-6.5}/django_modelcluster.egg-info/requires.txt +0 -0
- {django-modelcluster-6.4 → django_modelcluster-6.5}/django_modelcluster.egg-info/top_level.txt +0 -0
- {django-modelcluster-6.4 → django_modelcluster-6.5}/modelcluster/__init__.py +0 -0
- {django-modelcluster-6.4 → django_modelcluster-6.5}/modelcluster/contrib/__init__.py +0 -0
|
@@ -1,6 +1,22 @@
|
|
|
1
1
|
Changelog
|
|
2
2
|
=========
|
|
3
3
|
|
|
4
|
+
6.5 (01.05.2026)
|
|
5
|
+
~~~~~~~~~~~~~~~~
|
|
6
|
+
* Preliminary Django 6.1 support (Sage Abdullah)
|
|
7
|
+
* Implement `none` method on FakeQuerySet (Victor Miti)
|
|
8
|
+
* Fix: Recursively copy child relations on `copy_cluster`, `copy_all_child_relations` and `copy_child_relation` (Jakub Musil, Matt Westcott)
|
|
9
|
+
* Fix: Fix behavior of ParentalManyToManyFields and `prefetch_related()` supplied with a lookup queryset (Bernhard Bliem)
|
|
10
|
+
* Fix: Prevent `ClusterForm` creating duplicates when unsaved child objects exist on both initial instance and form data (Alex Tomkins)
|
|
11
|
+
* Maintenance: Migrate setup.py to pyproject.toml (Storm Heg)
|
|
12
|
+
* Maintenance: Add tooling for linting / formatting (Storm Heg)
|
|
13
|
+
* Maintenance: Set up nightly testing against Django main branch (Storm Heg)
|
|
14
|
+
* Maintenance: Set up PyPI trusted publishing (Storm Heg)
|
|
15
|
+
|
|
16
|
+
6.4.1 (04.12.2025)
|
|
17
|
+
~~~~~~~~~~~~~~~~~~
|
|
18
|
+
* Handle `get_prefetch_queryset` deprecation, for Django 6.0 support (Sage Abdullah)
|
|
19
|
+
|
|
4
20
|
6.4 (18.12.2024)
|
|
5
21
|
~~~~~~~~~~~~~~~~
|
|
6
22
|
* Add `UniqueConstraint` support for uniqueness validation (Sage Abdullah)
|
|
@@ -1,28 +1,31 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: django-modelcluster
|
|
3
|
-
Version: 6.
|
|
3
|
+
Version: 6.5
|
|
4
4
|
Summary: Django extension to allow working with 'clusters' of models as a single unit, independently of the database
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
Author-email: Matthew Westcott <matthew.westcott@torchbox.com>, Wagtail Team <hello@wagtail.org>
|
|
6
|
+
License-Expression: BSD-3-Clause
|
|
7
|
+
Project-URL: Changelog, https://github.com/wagtail/django-modelcluster/blob/main/CHANGELOG.txt
|
|
8
|
+
Project-URL: Homepage, https://github.com/wagtail/django-modelcluster
|
|
9
|
+
Keywords: cluster,django,model
|
|
9
10
|
Classifier: Development Status :: 5 - Production/Stable
|
|
10
11
|
Classifier: Environment :: Web Environment
|
|
12
|
+
Classifier: Framework :: Django
|
|
11
13
|
Classifier: Intended Audience :: Developers
|
|
12
|
-
Classifier: License :: OSI Approved :: BSD License
|
|
13
14
|
Classifier: Operating System :: OS Independent
|
|
14
15
|
Classifier: Programming Language :: Python
|
|
15
|
-
Classifier: Programming Language :: Python :: 3
|
|
16
|
-
Classifier: Programming Language :: Python :: 3.9
|
|
16
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
17
17
|
Classifier: Programming Language :: Python :: 3.10
|
|
18
18
|
Classifier: Programming Language :: Python :: 3.11
|
|
19
19
|
Classifier: Programming Language :: Python :: 3.12
|
|
20
20
|
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
-
Classifier: Programming Language :: Python :: 3
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
Provides-Extra: taggit
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
22
|
+
Requires-Python: >=3.10
|
|
23
|
+
Description-Content-Type: text/x-rst
|
|
25
24
|
License-File: LICENSE
|
|
25
|
+
Requires-Dist: django>=4.2
|
|
26
|
+
Provides-Extra: taggit
|
|
27
|
+
Requires-Dist: django-taggit>=3.1; extra == "taggit"
|
|
28
|
+
Dynamic: license-file
|
|
26
29
|
|
|
27
30
|
django-modelcluster
|
|
28
31
|
===================
|
|
@@ -60,11 +63,11 @@ But what if you could? There are all sorts of scenarios where you might want to
|
|
|
60
63
|
**django-modelcluster** extends Django's foreign key relations to make this possible. It introduces a new type of relation, *ParentalKey*, where the related models are stored locally to the 'parent' model until the parent is explicitly saved. Up to that point, the related models can still be accessed through a subset of the QuerySet API:
|
|
61
64
|
|
|
62
65
|
.. code-block:: python
|
|
63
|
-
|
|
66
|
+
|
|
64
67
|
from modelcluster.models import ClusterableModel
|
|
65
68
|
from modelcluster.fields import ParentalKey
|
|
66
|
-
|
|
67
|
-
|
|
69
|
+
|
|
70
|
+
|
|
68
71
|
class Band(ClusterableModel):
|
|
69
72
|
name = models.CharField(max_length=255)
|
|
70
73
|
|
|
@@ -34,11 +34,11 @@ But what if you could? There are all sorts of scenarios where you might want to
|
|
|
34
34
|
**django-modelcluster** extends Django's foreign key relations to make this possible. It introduces a new type of relation, *ParentalKey*, where the related models are stored locally to the 'parent' model until the parent is explicitly saved. Up to that point, the related models can still be accessed through a subset of the QuerySet API:
|
|
35
35
|
|
|
36
36
|
.. code-block:: python
|
|
37
|
-
|
|
37
|
+
|
|
38
38
|
from modelcluster.models import ClusterableModel
|
|
39
39
|
from modelcluster.fields import ParentalKey
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
|
|
41
|
+
|
|
42
42
|
class Band(ClusterableModel):
|
|
43
43
|
name = models.CharField(max_length=255)
|
|
44
44
|
|
|
@@ -1,28 +1,31 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: django-modelcluster
|
|
3
|
-
Version: 6.
|
|
3
|
+
Version: 6.5
|
|
4
4
|
Summary: Django extension to allow working with 'clusters' of models as a single unit, independently of the database
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
Author-email: Matthew Westcott <matthew.westcott@torchbox.com>, Wagtail Team <hello@wagtail.org>
|
|
6
|
+
License-Expression: BSD-3-Clause
|
|
7
|
+
Project-URL: Changelog, https://github.com/wagtail/django-modelcluster/blob/main/CHANGELOG.txt
|
|
8
|
+
Project-URL: Homepage, https://github.com/wagtail/django-modelcluster
|
|
9
|
+
Keywords: cluster,django,model
|
|
9
10
|
Classifier: Development Status :: 5 - Production/Stable
|
|
10
11
|
Classifier: Environment :: Web Environment
|
|
12
|
+
Classifier: Framework :: Django
|
|
11
13
|
Classifier: Intended Audience :: Developers
|
|
12
|
-
Classifier: License :: OSI Approved :: BSD License
|
|
13
14
|
Classifier: Operating System :: OS Independent
|
|
14
15
|
Classifier: Programming Language :: Python
|
|
15
|
-
Classifier: Programming Language :: Python :: 3
|
|
16
|
-
Classifier: Programming Language :: Python :: 3.9
|
|
16
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
17
17
|
Classifier: Programming Language :: Python :: 3.10
|
|
18
18
|
Classifier: Programming Language :: Python :: 3.11
|
|
19
19
|
Classifier: Programming Language :: Python :: 3.12
|
|
20
20
|
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
-
Classifier: Programming Language :: Python :: 3
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
Provides-Extra: taggit
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
22
|
+
Requires-Python: >=3.10
|
|
23
|
+
Description-Content-Type: text/x-rst
|
|
25
24
|
License-File: LICENSE
|
|
25
|
+
Requires-Dist: django>=4.2
|
|
26
|
+
Provides-Extra: taggit
|
|
27
|
+
Requires-Dist: django-taggit>=3.1; extra == "taggit"
|
|
28
|
+
Dynamic: license-file
|
|
26
29
|
|
|
27
30
|
django-modelcluster
|
|
28
31
|
===================
|
|
@@ -60,11 +63,11 @@ But what if you could? There are all sorts of scenarios where you might want to
|
|
|
60
63
|
**django-modelcluster** extends Django's foreign key relations to make this possible. It introduces a new type of relation, *ParentalKey*, where the related models are stored locally to the 'parent' model until the parent is explicitly saved. Up to that point, the related models can still be accessed through a subset of the QuerySet API:
|
|
61
64
|
|
|
62
65
|
.. code-block:: python
|
|
63
|
-
|
|
66
|
+
|
|
64
67
|
from modelcluster.models import ClusterableModel
|
|
65
68
|
from modelcluster.fields import ParentalKey
|
|
66
|
-
|
|
67
|
-
|
|
69
|
+
|
|
70
|
+
|
|
68
71
|
class Band(ClusterableModel):
|
|
69
72
|
name = models.CharField(max_length=255)
|
|
70
73
|
|
|
@@ -9,7 +9,9 @@ from modelcluster.queryset import FakeQuerySet
|
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
if TAGGIT_VERSION < (0, 20, 0):
|
|
12
|
-
raise Exception(
|
|
12
|
+
raise Exception(
|
|
13
|
+
"modelcluster.contrib.taggit requires django-taggit version 0.20 or above"
|
|
14
|
+
)
|
|
13
15
|
|
|
14
16
|
|
|
15
17
|
class _ClusterTaggableManager(_TaggableManager):
|
|
@@ -20,7 +22,9 @@ class _ClusterTaggableManager(_TaggableManager):
|
|
|
20
22
|
DeferringRelatedManager which allows writing related objects without committing them
|
|
21
23
|
to the database.
|
|
22
24
|
"""
|
|
23
|
-
rel_name = self.through._meta.get_field(
|
|
25
|
+
rel_name = self.through._meta.get_field(
|
|
26
|
+
"content_object"
|
|
27
|
+
).remote_field.get_accessor_name()
|
|
24
28
|
return getattr(self.instance, rel_name)
|
|
25
29
|
|
|
26
30
|
def get_queryset(self, extra_filters=None):
|
|
@@ -114,7 +118,8 @@ class _ClusterTaggableManager(_TaggableManager):
|
|
|
114
118
|
|
|
115
119
|
tagged_item_manager = self.get_tagged_item_manager()
|
|
116
120
|
tagged_items = [
|
|
117
|
-
tagged_item
|
|
121
|
+
tagged_item
|
|
122
|
+
for tagged_item in tagged_item_manager.all()
|
|
118
123
|
if tagged_item.tag.name in tags
|
|
119
124
|
]
|
|
120
125
|
tagged_item_manager.remove(*tagged_items)
|
|
@@ -149,7 +154,10 @@ class ClusterTaggableManager(TaggableManager):
|
|
|
149
154
|
# override TaggableManager's requirement for instance to have a primary key
|
|
150
155
|
# before we can access its tags
|
|
151
156
|
manager = _ClusterTaggableManager(
|
|
152
|
-
through=self.through,
|
|
157
|
+
through=self.through,
|
|
158
|
+
model=model,
|
|
159
|
+
instance=instance,
|
|
160
|
+
prefetch_cache_name=self.name,
|
|
153
161
|
)
|
|
154
162
|
|
|
155
163
|
return manager
|
|
@@ -158,8 +166,10 @@ class ClusterTaggableManager(TaggableManager):
|
|
|
158
166
|
# retrieve the queryset via the related manager on the content object,
|
|
159
167
|
# to accommodate the possibility of this having uncommitted changes relative to
|
|
160
168
|
# the live database
|
|
161
|
-
rel_name = self.through._meta.get_field(
|
|
169
|
+
rel_name = self.through._meta.get_field(
|
|
170
|
+
"content_object"
|
|
171
|
+
).remote_field.get_accessor_name()
|
|
162
172
|
ret = getattr(instance, rel_name).all()
|
|
163
|
-
if TAGGIT_VERSION >= (1,
|
|
173
|
+
if TAGGIT_VERSION >= (1,): # expects a Tag list instead of TaggedItem List
|
|
164
174
|
ret = [tagged_item.tag for tagged_item in ret]
|
|
165
175
|
return ret
|
|
@@ -15,9 +15,7 @@ DATEFIELD_TRANSFORM_EXPRESSIONS = {
|
|
|
15
15
|
"quarter",
|
|
16
16
|
}
|
|
17
17
|
DATETIMEFIELD_TRANSFORM_EXPRESSIONS = (
|
|
18
|
-
{"date", "time"}
|
|
19
|
-
| TIMEFIELD_TRANSFORM_EXPRESSIONS
|
|
20
|
-
| DATEFIELD_TRANSFORM_EXPRESSIONS
|
|
18
|
+
{"date", "time"} | TIMEFIELD_TRANSFORM_EXPRESSIONS | DATEFIELD_TRANSFORM_EXPRESSIONS
|
|
21
19
|
)
|
|
22
20
|
TRANSFORM_FIELD_TYPES = {
|
|
23
21
|
"year": forms.IntegerField,
|
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
from __future__ import unicode_literals
|
|
2
2
|
|
|
3
|
+
from django import VERSION as DJANGO_VERSION
|
|
3
4
|
from django.core import checks
|
|
4
5
|
from django.db import IntegrityError, connections, router
|
|
5
6
|
from django.db.models import CASCADE
|
|
6
7
|
from django.db.models.fields.related import ForeignKey, ManyToManyField
|
|
7
8
|
from django.utils.functional import cached_property
|
|
8
9
|
|
|
9
|
-
from django.db.models.fields.related import
|
|
10
|
+
from django.db.models.fields.related import (
|
|
11
|
+
ReverseManyToOneDescriptor,
|
|
12
|
+
ManyToManyDescriptor,
|
|
13
|
+
)
|
|
10
14
|
|
|
11
15
|
|
|
12
16
|
from modelcluster.utils import sort_by_fields
|
|
@@ -36,7 +40,7 @@ def create_deferring_foreign_related_manager(related, original_manager_cls):
|
|
|
36
40
|
@property
|
|
37
41
|
def is_deferring(self):
|
|
38
42
|
return relation_name in getattr(
|
|
39
|
-
self.instance,
|
|
43
|
+
self.instance, "_cluster_related_objects", {}
|
|
40
44
|
)
|
|
41
45
|
|
|
42
46
|
def _get_cluster_related_objects(self):
|
|
@@ -84,7 +88,12 @@ def create_deferring_foreign_related_manager(related, original_manager_cls):
|
|
|
84
88
|
# to work correctly with prefetch
|
|
85
89
|
return queryset._next_is_sticky().all()
|
|
86
90
|
|
|
87
|
-
def
|
|
91
|
+
def get_prefetch_querysets(self, instances, querysets=None):
|
|
92
|
+
if querysets and len(querysets) != 1:
|
|
93
|
+
raise ValueError(
|
|
94
|
+
"get_prefetch_querysets() must be passed exactly one queryset"
|
|
95
|
+
)
|
|
96
|
+
queryset = querysets[0] if querysets else None
|
|
88
97
|
if queryset is None:
|
|
89
98
|
db = self._db or router.db_for_read(self.model, instance=instances[0])
|
|
90
99
|
queryset = super().get_queryset().using(db)
|
|
@@ -93,7 +102,7 @@ def create_deferring_foreign_related_manager(related, original_manager_cls):
|
|
|
93
102
|
instance_attr = rel_field.get_foreign_related_value
|
|
94
103
|
instances_dict = dict((instance_attr(inst), inst) for inst in instances)
|
|
95
104
|
|
|
96
|
-
query = {
|
|
105
|
+
query = {"%s__in" % rel_field.name: instances}
|
|
97
106
|
qs = queryset.filter(**query)
|
|
98
107
|
# Since we just bypassed this class' get_queryset(), we must manage
|
|
99
108
|
# the reverse relation manually.
|
|
@@ -103,6 +112,14 @@ def create_deferring_foreign_related_manager(related, original_manager_cls):
|
|
|
103
112
|
cache_name = rel_field.related_query_name()
|
|
104
113
|
return qs, rel_obj_attr, instance_attr, False, cache_name, False
|
|
105
114
|
|
|
115
|
+
# Remove once we only support Django 5.0+
|
|
116
|
+
if not DJANGO_VERSION >= (5, 0):
|
|
117
|
+
|
|
118
|
+
def get_prefetch_queryset(self, instances, queryset=None):
|
|
119
|
+
if queryset is None:
|
|
120
|
+
return self.get_prefetch_querysets(instances)
|
|
121
|
+
return self.get_prefetch_querysets(instances, querysets=[queryset])
|
|
122
|
+
|
|
106
123
|
def get_object_list(self):
|
|
107
124
|
"""
|
|
108
125
|
return the mutable list that forms the current in-memory state of
|
|
@@ -199,7 +216,9 @@ def create_deferring_foreign_related_manager(related, original_manager_cls):
|
|
|
199
216
|
from the database.
|
|
200
217
|
"""
|
|
201
218
|
if self.instance.pk is None:
|
|
202
|
-
raise IntegrityError(
|
|
219
|
+
raise IntegrityError(
|
|
220
|
+
"Cannot commit relation %r on an unsaved model" % relation_name
|
|
221
|
+
)
|
|
203
222
|
|
|
204
223
|
try:
|
|
205
224
|
final_items = self.instance._cluster_related_objects[relation_name]
|
|
@@ -241,14 +260,16 @@ class ChildObjectsDescriptor(ReverseManyToOneDescriptor):
|
|
|
241
260
|
|
|
242
261
|
@cached_property
|
|
243
262
|
def child_object_manager_cls(self):
|
|
244
|
-
return create_deferring_foreign_related_manager(
|
|
263
|
+
return create_deferring_foreign_related_manager(
|
|
264
|
+
self.rel, self.related_manager_cls
|
|
265
|
+
)
|
|
245
266
|
|
|
246
267
|
|
|
247
268
|
class ParentalKey(ForeignKey):
|
|
248
269
|
related_accessor_class = ChildObjectsDescriptor
|
|
249
270
|
|
|
250
271
|
def __init__(self, *args, **kwargs):
|
|
251
|
-
kwargs.setdefault(
|
|
272
|
+
kwargs.setdefault("on_delete", CASCADE)
|
|
252
273
|
super().__init__(*args, **kwargs)
|
|
253
274
|
|
|
254
275
|
def check(self, **kwargs):
|
|
@@ -260,26 +281,30 @@ class ParentalKey(ForeignKey):
|
|
|
260
281
|
# If self.rel.to is a string at this point, it means that Django has been unable
|
|
261
282
|
# to resolve it as a model name; if so, skip this test so that Django's own
|
|
262
283
|
# system checks can report the appropriate error
|
|
263
|
-
if isinstance(self.remote_field.model, type) and not issubclass(
|
|
284
|
+
if isinstance(self.remote_field.model, type) and not issubclass(
|
|
285
|
+
self.remote_field.model, ClusterableModel
|
|
286
|
+
):
|
|
264
287
|
errors.append(
|
|
265
288
|
checks.Error(
|
|
266
|
-
|
|
267
|
-
hint=
|
|
268
|
-
model_name=self.remote_field.model._meta.app_label
|
|
289
|
+
"ParentalKey must point to a subclass of ClusterableModel.",
|
|
290
|
+
hint="Change {model_name} into a ClusterableModel or use a ForeignKey instead.".format(
|
|
291
|
+
model_name=self.remote_field.model._meta.app_label
|
|
292
|
+
+ "."
|
|
293
|
+
+ self.remote_field.model.__name__,
|
|
269
294
|
),
|
|
270
295
|
obj=self,
|
|
271
|
-
id=
|
|
296
|
+
id="modelcluster.E001",
|
|
272
297
|
)
|
|
273
298
|
)
|
|
274
299
|
|
|
275
300
|
# ParentalKeys must have an accessor name (#49)
|
|
276
|
-
if self.remote_field.get_accessor_name() ==
|
|
301
|
+
if self.remote_field.get_accessor_name() == "+":
|
|
277
302
|
errors.append(
|
|
278
303
|
checks.Error(
|
|
279
304
|
"related_name='+' is not allowed on ParentalKey fields",
|
|
280
305
|
hint="Either change it to a valid name or remove it",
|
|
281
306
|
obj=self,
|
|
282
|
-
id=
|
|
307
|
+
id="modelcluster.E002",
|
|
283
308
|
)
|
|
284
309
|
)
|
|
285
310
|
|
|
@@ -338,15 +363,20 @@ def create_deferring_forward_many_to_many_manager(rel, original_manager_cls):
|
|
|
338
363
|
|
|
339
364
|
return FakeQuerySet(rel_model, results)
|
|
340
365
|
|
|
341
|
-
def
|
|
366
|
+
def get_prefetch_querysets(self, instances, querysets=None):
|
|
342
367
|
# Derived from Django's ManyRelatedManager.get_prefetch_queryset.
|
|
368
|
+
if querysets and len(querysets) != 1:
|
|
369
|
+
raise ValueError(
|
|
370
|
+
"get_prefetch_querysets() must be passed exactly one queryset"
|
|
371
|
+
)
|
|
372
|
+
queryset = querysets[0] if querysets else None
|
|
343
373
|
if queryset is None:
|
|
344
374
|
queryset = super().get_queryset()
|
|
345
375
|
|
|
346
376
|
queryset._add_hints(instance=instances[0])
|
|
347
377
|
queryset = queryset.using(queryset._db or self._db)
|
|
348
378
|
|
|
349
|
-
query = {
|
|
379
|
+
query = {"%s__in" % query_field_name: instances}
|
|
350
380
|
queryset = queryset._next_is_sticky().filter(**query)
|
|
351
381
|
|
|
352
382
|
fk = self.through._meta.get_field(source_field_name)
|
|
@@ -355,14 +385,18 @@ def create_deferring_forward_many_to_many_manager(rel, original_manager_cls):
|
|
|
355
385
|
connection = connections[queryset.db]
|
|
356
386
|
qn = connection.ops.quote_name
|
|
357
387
|
|
|
358
|
-
queryset = queryset.extra(
|
|
359
|
-
|
|
360
|
-
|
|
388
|
+
queryset = queryset.extra(
|
|
389
|
+
select={
|
|
390
|
+
"_prefetch_related_val_%s" % f.attname: "%s.%s"
|
|
391
|
+
% (qn(join_table), qn(f.column))
|
|
392
|
+
for f in fk.local_related_fields
|
|
393
|
+
}
|
|
394
|
+
)
|
|
361
395
|
|
|
362
396
|
return (
|
|
363
397
|
queryset,
|
|
364
398
|
lambda result: tuple(
|
|
365
|
-
getattr(result,
|
|
399
|
+
getattr(result, "_prefetch_related_val_%s" % f.attname)
|
|
366
400
|
for f in fk.local_related_fields
|
|
367
401
|
),
|
|
368
402
|
lambda inst: tuple(
|
|
@@ -374,9 +408,19 @@ def create_deferring_forward_many_to_many_manager(rel, original_manager_cls):
|
|
|
374
408
|
False,
|
|
375
409
|
)
|
|
376
410
|
|
|
411
|
+
# Remove once we only support Django 5.0+
|
|
412
|
+
if not DJANGO_VERSION >= (5, 0):
|
|
413
|
+
|
|
414
|
+
def get_prefetch_queryset(self, instances, queryset=None):
|
|
415
|
+
if queryset is None:
|
|
416
|
+
return self.get_prefetch_querysets(instances)
|
|
417
|
+
return self.get_prefetch_querysets(instances, querysets=[queryset])
|
|
418
|
+
|
|
377
419
|
def _apply_rel_filters(self, queryset):
|
|
378
420
|
# Required for get_prefetch_queryset.
|
|
379
|
-
return queryset
|
|
421
|
+
# NOTE: _apply_rel_filters() must return a copy of the queryset
|
|
422
|
+
# to work correctly with prefetch
|
|
423
|
+
return queryset._next_is_sticky().all()
|
|
380
424
|
|
|
381
425
|
def get_object_list(self):
|
|
382
426
|
"""
|
|
@@ -404,8 +448,10 @@ def create_deferring_forward_many_to_many_manager(rel, original_manager_cls):
|
|
|
404
448
|
|
|
405
449
|
for target in new_items:
|
|
406
450
|
if target.pk is None:
|
|
407
|
-
raise ValueError(
|
|
408
|
-
'
|
|
451
|
+
raise ValueError(
|
|
452
|
+
'"%r" needs to have a primary key value before '
|
|
453
|
+
"it can be added to a parental many-to-many relation." % target
|
|
454
|
+
)
|
|
409
455
|
item_matched = False
|
|
410
456
|
for i, item in enumerate(items):
|
|
411
457
|
if item == target:
|
|
@@ -430,7 +476,9 @@ def create_deferring_forward_many_to_many_manager(rel, original_manager_cls):
|
|
|
430
476
|
"""
|
|
431
477
|
self.set([])
|
|
432
478
|
|
|
433
|
-
def
|
|
479
|
+
def set_base(
|
|
480
|
+
self, objs, bulk=True, clear=False, through_defaults=None, raw=False
|
|
481
|
+
):
|
|
434
482
|
# cast objs to a list so that:
|
|
435
483
|
# 1) we can call len() on it (which we can't do on, say, a queryset)
|
|
436
484
|
# 2) if we need to sort it, we can do so without mutating the original
|
|
@@ -451,6 +499,9 @@ def create_deferring_forward_many_to_many_manager(rel, original_manager_cls):
|
|
|
451
499
|
|
|
452
500
|
cluster_related_objects[relation_name] = objs
|
|
453
501
|
|
|
502
|
+
def set(self, objs, *, clear=False, through_defaults=None):
|
|
503
|
+
self.set_base(objs, clear=clear, through_defaults=through_defaults)
|
|
504
|
+
|
|
454
505
|
def remove(self, *items_to_remove):
|
|
455
506
|
"""
|
|
456
507
|
Remove the passed items from the stored object set, but do not commit the change
|
|
@@ -466,7 +517,9 @@ def create_deferring_forward_many_to_many_manager(rel, original_manager_cls):
|
|
|
466
517
|
Apply any changes made to the stored object set to the database.
|
|
467
518
|
"""
|
|
468
519
|
if not self.instance.pk:
|
|
469
|
-
raise IntegrityError(
|
|
520
|
+
raise IntegrityError(
|
|
521
|
+
"Cannot commit relation %r on an unsaved model" % relation_name
|
|
522
|
+
)
|
|
470
523
|
|
|
471
524
|
try:
|
|
472
525
|
final_items = self.instance._cluster_related_objects[relation_name]
|
|
@@ -506,7 +559,9 @@ class ParentalManyToManyDescriptor(ManyToManyDescriptor):
|
|
|
506
559
|
def child_object_manager_cls(self):
|
|
507
560
|
rel = self.rel
|
|
508
561
|
|
|
509
|
-
return create_deferring_forward_many_to_many_manager(
|
|
562
|
+
return create_deferring_forward_many_to_many_manager(
|
|
563
|
+
rel, self.related_manager_cls
|
|
564
|
+
)
|
|
510
565
|
|
|
511
566
|
|
|
512
567
|
class ParentalManyToManyField(ManyToManyField):
|