I have the following models:
class Camera(models.Model)
deleted_images_counter = models.IntegerField(...)
class Image(models.Model)
image = models.ImageField(....)
camera = models.ForeignKey(Camera)
Now, i want to update the camera deleted_images_counter field when i delete one or more images.
I though about using signals:
@receiver(pre_delete, sender=Image,dispatch_uid="test" )
def update_deleted_images_counter(sender, instance, using, **kwargs):
camera = instance.camera
camera.counter = camera.counter + 1
camera.save()
But the problem is that it works at object level, so if i want to delete 200,000 images, it have to make the same amount of updates to the camera.
Is there any way i create a new method or a signal where i can do something like this?
images_to_delete = Images.objects.filter(...)
images_to_delete.delete()
#some method of something
def delete(queryset, ....):
#update the counter
camera.deleted_images_counter = camera.deleted_images_counter + queryset.count()
camera.save()
#continue with the delete...
queryset.delete()
Thanks!
Edit 1:
I have just noticed a flaw on what i want to do. I was assuming that the images to delete belonged to the same camera, however if they are not from the same camera then this approach would not work. The only way to achieve what i want would be in the listener i guess...