mirror of
https://github.com/django/django.git
synced 2025-10-31 09:41:08 +00:00
Fixed #30325 -- Reverted "Fixed #29725 -- Removed unnecessary join in QuerySet.count() and exists() on a many-to-many relation."
This reverts commit 1299421cad due to
a regression with custom managers.
This commit is contained in:
@@ -929,33 +929,6 @@ def create_forward_many_to_many_manager(superclass, rel, reverse):
|
||||
False,
|
||||
)
|
||||
|
||||
@property
|
||||
def constrained_target(self):
|
||||
# If the through relation's target field's foreign integrity is
|
||||
# enforced, the query can be performed solely against the through
|
||||
# table as the INNER JOIN'ing against target table is unnecessary.
|
||||
if not self.target_field.db_constraint:
|
||||
return None
|
||||
db = router.db_for_read(self.through, instance=self.instance)
|
||||
if not connections[db].features.supports_foreign_keys:
|
||||
return None
|
||||
hints = {'instance': self.instance}
|
||||
manager = self.through._base_manager.db_manager(db, hints=hints)
|
||||
filters = {self.source_field_name: self.instance.pk}
|
||||
# Nullable target rows must be excluded as well as they would have
|
||||
# been filtered out from an INNER JOIN.
|
||||
if self.target_field.null:
|
||||
filters['%s__isnull' % self.target_field_name] = False
|
||||
return manager.filter(**filters)
|
||||
|
||||
def exists(self):
|
||||
constrained_target = self.constrained_target
|
||||
return constrained_target.exists() if constrained_target else super().exists()
|
||||
|
||||
def count(self):
|
||||
constrained_target = self.constrained_target
|
||||
return constrained_target.count() if constrained_target else super().count()
|
||||
|
||||
def add(self, *objs, through_defaults=None):
|
||||
self._remove_prefetched_objects()
|
||||
db = router.db_for_write(self.through, instance=self.instance)
|
||||
|
||||
Reference in New Issue
Block a user