rsinger86/django-lifecycle

changes on related models don't trigger the hooked methods

Closed this issue ยท 7 comments

I think you SHOULD update the documentation because it caused me to waste lot of time trying to figure out why foreign keys triggers are not working before reading that you don't support that and are not planning to do that either.

I have the same problem, fixing this would be a great improvement.

I think you SHOULD update the documentation

Yes, it would be nice to have a section in the documentation explaining common gotchas using this library.

why foreign keys triggers are not working before reading that you don't support that and are not planning to do that either

I have the same problem, fixing this would be a great improvement.

Can you explain what's your problem or how you would expect this library should behave so we can help you? @fabiocaccamo @zahersarieddine

@EnriqueSoria thanks for your reply, but after more than a year since my comment I don't remember anymore the exact problem/scenario.

@EnriqueSoria thanks for your reply, but after more than a year since my comment I don't remember anymore the exact problem/scenario.

haha understandable... anyway, I had to try

I try to recover some info, I should have left some comments in a private project.

I may be wrong, but i think that the problem is that hooks don't work as expected when the watched field (parent in the example below) is a ForeignKey.

@hook(AFTER_SAVE, when="parent", was="")
def update_when_parent_is_set(self):
    # do stuff
    pass 

So, if the functionality is not fully supported, an error should be raised to avoid unnecessary debugging.

I may be wrong, but i think that the problem is that hooks don't work as expected when the watched field (parent in the example below) is a ForeignKey.

@hook(AFTER_SAVE, when="parent", was="")
def update_when_parent_is_set(self):
    # do stuff
    pass 

So, if the functionality is not fully supported, an error should be raised to avoid unnecessary debugging.

I have just tested that this works:

class Book(LifecycleModelMixin, models.Model):
    title = models.CharField(max_length=200)
    previous_book = models.ForeignKey(
        "Book", null=True, blank=True, on_delete=models.CASCADE
    )

    @hook(AFTER_SAVE, when="previous_book", was=None, has_changed=True)
    def update_when_parent_is_set(self):
        print("Changing title")

Also, there's a specific section in the docs about watching FK changes.