Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AFTER_DELETE hook on ManyToMany relationships #122

Closed
mateocpdev opened this issue Dec 4, 2022 · 1 comment
Closed

AFTER_DELETE hook on ManyToMany relationships #122

mateocpdev opened this issue Dec 4, 2022 · 1 comment

Comments

@mateocpdev
Copy link

Hi,
I'm writing this issue because I think the AFTER_DELETE hook does not work as expected on m2m relationships.
If we have something like that:

class Product(LifecycleModel):
	title = models.Charfield(max_length=100)
	images = models.ManyToManyField(
        to="ProductImage", through="ProductImageRelationship", related_name="products"
    )

class ProductImageRelationship(LifecycleModel):
    product = models.ForeignKey("Product", on_delete=models.CASCADE)
    image = models.ForeignKey("ProductImage", on_delete=models.CASCADE)
    order = models.IntegerField(default=0, help_text="Lower number, higher priority")

class ProductImage(LifecycleModel):
	field_name = models.CharField(max_length=40)

If I write a hook on ProductImageRelationship model like this:

    @hook(AFTER_DELETE, on_commit=True)
    def deleting_image(self):
        print("Image deleted...")

the hook is never triggered when I do

p = Product.objects.get(pk=123)
i = p.images.first()
# to remove image from product do
p.images.remove(i)
# or do this
i.products.remove(p)

However, If I add a receiver like this:

@receiver(post_delete, sender=ProductImageRelationship)
def deleting_image(sender, instance, **kwargs):
    print("Image deleted...")

The receiver is triggered as is expected.

I think I'm doing it correctly 😕 but I'm not sure completely.

@madtools
Copy link

Refer to issue #120, it is not possible to use Django-lifecycle with a through model for a Many to Many.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants