#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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018faa15b450-87fdca1c-8670-4784-a40b-e386e873a621-000000%40eu-central-1.amazonses.com.

Reply via email to