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

Preserve instance.pk in pre_save signal handler #260

Open
Alex-Sichkar opened this issue Oct 10, 2023 · 0 comments
Open

Preserve instance.pk in pre_save signal handler #260

Alex-Sichkar opened this issue Oct 10, 2023 · 0 comments

Comments

@Alex-Sichkar
Copy link

Alex-Sichkar commented Oct 10, 2023

Hi there! Thanks for a very useful project.

I believe that instance.pk should be preserved in the pre_save signal handler before passing to a transaction.on_commit() callback. There are cases, such as when using transactions, where the instance might not have a primary key already. For instance:

class Parent(models.Model):
    ...


class Child(models.Model):
    parent = models.ForeignKey(Parent, ...)
    ...

    def delete(self):
        super().delete()
        self.parent.foo = 'bar'
        self.parent.save()


@transaction.atomic
def some_function(parent):
    for child in parent.child_set.all():
        ...
        child.delete()
    parent.delete()

The callback runs only after the outermost transaction has been committed, but the parent has already been deleted and does not have a primary key at that moment.

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

1 participant