Search code examples
djangodjango-formsdjango-signals

Are pre-save signals handled before the clean method in Django?


I could not find a reference to it. The question is honestly pretty self-explanatory. In Django forms, such as admin forms, the clean method is automatically called before saving. My question is, if I have a method as a pre-save signal, which one will execute first?


Solution

  • According to the django docs: "This is sent at the beginning of a model’s save() method." You can find this here: https://docs.djangoproject.com/en/2.2/ref/signals/#pre-save and https://docs.djangoproject.com/en/2.2/topics/signals/#connecting-to-signals-sent-by-specific-senders

    The clean method works as a validation which means it always runs before the save method, you can find more information about this here: https://docs.djangoproject.com/en/2.2/ref/forms/validation/#form-and-field-validation

    You can also validate this on the Django admin code: https://github.com/django/django/blob/master/django/contrib/admin/options.py#L1545