Search code examples
pythondjangodjango-signals

Django pre_save signal - would an exception fail the transaction?


I want to do some custom actions before a user is created. and I thought of using the pre_save signal for that. And in case one of those action would raise an exception stop the transaction, abort creating the user etc.

  1. Is this the way to go? would it abort the save if something fails in this step (this is the required behavior) I suspect so but couldn't find docs about it.

  2. What is the best practice for getting the future user.id. from my understanding it doesn't exists yet in pre-save but I need it as input for some of the extra custom actions.

Thanks


Solution

  • From the docs:

    There's no way to tell what the value of an ID will be before you call save(), because the value is determined by your database, not by Django.

    So if your pre-save processing requires the user.id, I am afraid this is not possible.