0

I've created a custom image field for Django that automatically creates thumbnails and does some other stuff.

from django.db.models.fields.files import ImageFieldFile

class ImageWithThumbsFieldFile(ImageFieldFile):
    def __init__(self, *args, **kwargs):
        ...

Now I would like to automatically connect a post_delete signal whenever such a field is defined in a model. I know how to connect a post_delete signal manually when the model is defined. But i there a way to do that automatically whenever the custom field is used?

Simon Steinberger
  • 6,605
  • 5
  • 55
  • 97

1 Answers1

2

You can do it in the contribute_to_class() method:

class ImageWithThumbsFieldFile(ImageFieldFile):
    ...
    def contribute_to_class(self, cls, name, **kwargs):
        super(ImageWithThumbsFieldFile, self).contribute_to_class(
                                                   cls, name, **kwargs)
        post_delete.connect(on_delete_callback, sender=cls)
catavaran
  • 44,703
  • 8
  • 98
  • 85