Django Admin and its “object delete summary” view

Hey there!

Just recently, I ran into an intersting topic/issue in Django Admin.

  1. I have an “Image” model that has an ImageField and some image metadata
  2. I have a “Person” model, with name, picture, etc.
  3. The image files may contain “face tags” (that have their own model) that reference persons at a specific location on the image

When deleting a “Person” model, I do not want the associated face tags to be deleted (thus on_delete=models.SET_NULL).

When deleting a “Person” model, Django just sets the foreign key on the face tags to null.

It would be useful if Django gave me a summary of all face tags that will lose the reference to the person I’m currently deleting (just like it does for CASCADing fks).

Long story short — any ideas how to make Django Admin show me the related models that will lose their reference to the current object?

The template where this happens is django/contrib/admin/templates/admin/delete_confirmation.html and django/contrib/admin/includes/object_delete_summary.html.

Thanks for reading & helping!

The Cubist

class Person(models.Model):
  name = models.Charfield(max_length=200, verbose_name="...") # and so forth

class Image(models.Model):
    media_file = models.ImageField(…)

class FaceTag(models.Model):
    image = models.ForeignKey(Image, on_delete=models.CASCADE)
    person = models.ForeignKey(Person, on_delete=models.SET_NULL)

    x = models.PositiveIntegerField(...)
    y = models.PositiveIntegerField(...)
    width = models.PositiveIntegerField(...)
    height = models.PositiveIntegerField(...)
1 Like

It looks like you could create a custom get_deleted_objects method in your ModelAdmin class to include the list of items having the FaceTags being set to null.

See the standard implementation in django.contrib.admin.utils as an example.

Ken

2 Likes

Thanks! Sounds good to me.