Add 'objs' to the collection of objects to be deleted as well as all parent instances. 'objs' must be a homogeneous iterable collection of model instances (e.g. a QuerySet). If 'collect_related' is True, related objects will be handled by their respective on_delete h
(
self,
objs,
source=None,
nullable=False,
collect_related=True,
source_attr=None,
reverse_dependency=False,
keep_parents=False,
fail_on_restricted=True,
)
| 269 | return [objs] |
| 270 | |
| 271 | def collect( |
| 272 | self, |
| 273 | objs, |
| 274 | source=None, |
| 275 | nullable=False, |
| 276 | collect_related=True, |
| 277 | source_attr=None, |
| 278 | reverse_dependency=False, |
| 279 | keep_parents=False, |
| 280 | fail_on_restricted=True, |
| 281 | ): |
| 282 | """ |
| 283 | Add 'objs' to the collection of objects to be deleted as well as all |
| 284 | parent instances. 'objs' must be a homogeneous iterable collection of |
| 285 | model instances (e.g. a QuerySet). If 'collect_related' is True, |
| 286 | related objects will be handled by their respective on_delete handler. |
| 287 | |
| 288 | If the call is the result of a cascade, 'source' should be the model |
| 289 | that caused it and 'nullable' should be set to True, if the relation |
| 290 | can be null. |
| 291 | |
| 292 | If 'reverse_dependency' is True, 'source' will be deleted before the |
| 293 | current model, rather than after. (Needed for cascading to parent |
| 294 | models, the one case in which the cascade follows the forwards |
| 295 | direction of an FK rather than the reverse direction.) |
| 296 | |
| 297 | If 'keep_parents' is True, data of parent model's will be not deleted. |
| 298 | |
| 299 | If 'fail_on_restricted' is False, error won't be raised even if it's |
| 300 | prohibited to delete such objects due to RESTRICT, that defers |
| 301 | restricted object checking in recursive calls where the top-level call |
| 302 | may need to collect more objects to determine whether restricted ones |
| 303 | can be deleted. |
| 304 | """ |
| 305 | if self.can_fast_delete(objs): |
| 306 | self.fast_deletes.append(objs) |
| 307 | return |
| 308 | new_objs = self.add( |
| 309 | objs, source, nullable, reverse_dependency=reverse_dependency |
| 310 | ) |
| 311 | if not new_objs: |
| 312 | return |
| 313 | |
| 314 | model = new_objs[0].__class__ |
| 315 | |
| 316 | if not keep_parents: |
| 317 | # Recursively collect concrete model's parent models, but not their |
| 318 | # related objects. These will be found by meta.get_fields() |
| 319 | concrete_model = model._meta.concrete_model |
| 320 | for ptr in concrete_model._meta.parents.values(): |
| 321 | if ptr: |
| 322 | parent_objs = [getattr(obj, ptr.name) for obj in new_objs] |
| 323 | self.collect( |
| 324 | parent_objs, |
| 325 | source=model, |
| 326 | source_attr=ptr.remote_field.related_name, |
| 327 | collect_related=False, |
| 328 | reverse_dependency=True, |