[Django] #35478: GenericRelation always deletes the related objects when a model object is deleted

10 views
Skip to first unread message

Django

unread,
May 24, 2024, 6:11:45 AM5/24/24
to django-...@googlegroups.com
#35478: GenericRelation always deletes the related objects when a model object is
deleted
-------------------------------------+-------------------------------------
Reporter: Brandon | Owner: nobody
Type: | Status: new
Uncategorized |
Component: | Version: 4.2
contrib.contenttypes | Keywords: genericrelation
Severity: Normal | genericforeignkey on_delete
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
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
[[https://github.com/django/django/blob/4.2.13/django/contrib/contenttypes/fields.py#L333|on_delete
is forced to models.CASCADE]] but
[[https://github.com/django/django/blob/4.2.13/django/contrib/contenttypes/fields.py#L290|GenricRel
forces it to DO_NOTHING]] anyway.

Either way on_delete seems to be totally ignored because the related
objects are part of
[[https://github.com/django/django/blob/4.2.13/django/db/models/deletion.py#L369|_meta.private_fields]]
and are deleted anyway.

The only way I've found to avoid this is to create a custom queryset like
this


{{{#!python
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"
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35478>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
May 24, 2024, 7:57:15 AM5/24/24
to django-...@googlegroups.com
#35478: GenericRelation always deletes the related objects when a model object is
deleted
-------------------------------------+-------------------------------------
Reporter: Brandon | Owner: nobody
Type: Uncategorized | Status: closed
Component: | Version: 4.2
contrib.contenttypes |
Severity: Normal | Resolution: duplicate
Keywords: genericrelation | Triage Stage:
genericforeignkey on_delete | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* resolution: => duplicate
* status: new => closed

Comment:

This is a bug and duplicate of #26722
--
Ticket URL: <https://code.djangoproject.com/ticket/35478#comment:1>
Reply all
Reply to author
Forward
0 new messages