Ticket #13513: collect_sub_objects.patch
File collect_sub_objects.patch, 2.5 KB (added by , 15 years ago) |
---|
-
db/models/base.py
544 544 545 545 save_base.alters_data = True 546 546 547 def _collect_sub_objects(self, seen_objs, parent=None, nullable=False):547 def _collect_sub_objects(self, seen_objs, using=None, parent=None, nullable=False): 548 548 """ 549 549 Recursively populates seen_objs with all objects related to this 550 550 object. … … 553 553 [(model_class, {pk_val: obj, pk_val: obj, ...}), 554 554 (model_class, {pk_val: obj, pk_val: obj, ...}), ...] 555 555 """ 556 using = using or router.db_for_write(self.__class__, instance=self) 557 556 558 pk_val = self._get_pk_val() 557 559 if seen_objs.add(self.__class__, pk_val, self, 558 560 type(parent), parent, nullable): … … 592 594 reverse_field_name = related.field.m2m_reverse_field_name() 593 595 nullable = opts.get_field(reverse_field_name).null 594 596 filters = {reverse_field_name: self} 595 for sub_obj in related.field.rel.through._base_manager. filter(**filters):597 for sub_obj in related.field.rel.through._base_manager.using(using).filter(**filters): 596 598 sub_obj._collect_sub_objects(seen_objs, self, nullable) 597 599 598 600 for f in self._meta.many_to_many: … … 601 603 field_name = f.m2m_field_name() 602 604 nullable = opts.get_field(field_name).null 603 605 filters = {field_name: self} 604 for sub_obj in f.rel.through._base_manager. filter(**filters):606 for sub_obj in f.rel.through._base_manager.using(using).filter(**filters): 605 607 sub_obj._collect_sub_objects(seen_objs, self, nullable) 606 608 else: 607 609 # m2m-ish but with no through table? GenericRelation: cascade delete … … 627 629 628 630 def delete(self, using=None): 629 631 using = using or router.db_for_write(self.__class__, instance=self) 630 connection = connections[using] 632 631 633 assert self._get_pk_val() is not None, "%s object can't be deleted because its %s attribute is set to None." % (self._meta.object_name, self._meta.pk.attname) 632 634 633 635 # Find all the objects than need to be deleted. 634 636 seen_objs = CollectedObjects() 635 self._collect_sub_objects(seen_objs )637 self._collect_sub_objects(seen_objs, using) 636 638 637 639 # Actually delete the objects. 638 640 delete_objects(seen_objs, using)