Opened 7 months ago
Closed 7 months ago
#35478 closed Uncategorized (duplicate)
GenericRelation always deletes the related objects when a model object is deleted
Reported by: | Brandon | Owned by: | nobody |
---|---|---|---|
Component: | contrib.contenttypes | Version: | 4.2 |
Severity: | Normal | Keywords: | genericrelation genericforeignkey on_delete |
Cc: | Brandon | Triage Stage: | Unreviewed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Is there a reason why when using GenericRelation a behaviour like on_delete=models.CASCADE is forced?
I've looked at the code for GenericRelation and on_delete is forced to models.CASCADE but GenricRel forces it to DO_NOTHING anyway.
Either way on_delete seems to be totally ignored because the related objects are part of _meta.private_fields and are deleted anyway.
The only way I've found to avoid this is to create a custom queryset like this
class MyQuerySet(models.QuerySet): def delete(): pass def _raw_delete(): return 0 class MyModel(models.Model): content_type = models.ForeignKey(ContentType, on_delete=models.SET_NULL, null=True) object_id = models.PositiveIntegerField(null=True) content_object = GenericForeignKey('content_type', 'object_id') objects = models.Manager.from_queryset(MyQuerySet)() class Meta: base_manager_name = "objects"
Note:
See TracTickets
for help on using tickets.
This is a bug and duplicate of #26722