django-polymorphic icon indicating copy to clipboard operation
django-polymorphic copied to clipboard

Issue with `.delete(keep_parents=True)`

Open Danfs64 opened this issue 4 months ago • 2 comments

If we have a model structure like:

from polymorphic.models import PolymorphicModel

class Project(PolymorphicModel):
    topic = models.CharField(max_length=30)

class ArtProject(Project):
    artist = models.CharField(max_length=30)

and populate it a little, if we choose to delete an ArtProject, but keep the underlaying Project intact, we would use something like:

ArtProject.objects.first().delete(keep_parents=True)

This will delete only the ArtProject row on the database, keeping the Project row intact, but if we run Project.objects.all() we will not see the project, even if it clearly is there on the DB. I imagine this has something to do with the fact that, when a .delete(keep_parents=True) is run, the polymorphic_ctype_id still points to, in this case, ArtProject, instead of being updated to the parent Model

If we instead run Project.objects.non_polymorphic().all(), we will see the Project on the returned Queryset.

References:

  • .delete() documentation: https://docs.djangoproject.com/en/5.2/ref/models/instances/#django.db.models.Model.delete

Danfs64 avatar Jun 24 '25 20:06 Danfs64