What are the typical use cases for pre-save, post-save, pre-delete, post-delete signals in Django?












0














Could not find much in the docs here regarding use cases. However, I assume something along these lines:




  • Pre-Save: Used for calculated fields, example, we might want to calculate the age from date of birth and save that in the database.

  • Post-Save: Used for operations that involve other objects in the database, for example, updating a review might require a re-calculation of the average rating for the movie.

  • Pre-Delete: Honestly, no idea.

  • Post-Delete: Probably similar to post-save, in that it affects other models.


What are the most common use cases for these signals in general? Thanks.










share|improve this question



























    0














    Could not find much in the docs here regarding use cases. However, I assume something along these lines:




    • Pre-Save: Used for calculated fields, example, we might want to calculate the age from date of birth and save that in the database.

    • Post-Save: Used for operations that involve other objects in the database, for example, updating a review might require a re-calculation of the average rating for the movie.

    • Pre-Delete: Honestly, no idea.

    • Post-Delete: Probably similar to post-save, in that it affects other models.


    What are the most common use cases for these signals in general? Thanks.










    share|improve this question

























      0












      0








      0







      Could not find much in the docs here regarding use cases. However, I assume something along these lines:




      • Pre-Save: Used for calculated fields, example, we might want to calculate the age from date of birth and save that in the database.

      • Post-Save: Used for operations that involve other objects in the database, for example, updating a review might require a re-calculation of the average rating for the movie.

      • Pre-Delete: Honestly, no idea.

      • Post-Delete: Probably similar to post-save, in that it affects other models.


      What are the most common use cases for these signals in general? Thanks.










      share|improve this question













      Could not find much in the docs here regarding use cases. However, I assume something along these lines:




      • Pre-Save: Used for calculated fields, example, we might want to calculate the age from date of birth and save that in the database.

      • Post-Save: Used for operations that involve other objects in the database, for example, updating a review might require a re-calculation of the average rating for the movie.

      • Pre-Delete: Honestly, no idea.

      • Post-Delete: Probably similar to post-save, in that it affects other models.


      What are the most common use cases for these signals in general? Thanks.







      django django-signals






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 '18 at 14:42









      darkhorse

      1,42541845




      1,42541845
























          1 Answer
          1






          active

          oldest

          votes


















          2














          A very common use for pre_save is slugifying titles, e.g. on books, articles, or products. This is also one reason it's always better to keep a separate ID to key on in URLs, etc. rather than relying solely on slugs, otherwise you either can't change your slugs or need to work around the fact that persistent pages may not have persistent URLs, or have to support legacy URLs/redirects.



          post_save may also be used for firing off or enqueuing asynchronous events; if you keep your users synced with a separate service, for example, a successful change to the local user profile may need to be submitted to an API, an operation you wouldn't want to make the user wait for. This kind of thing could technically be done elsewhere, but putting it in this signal means you don't have to worry about implementing or invoking it anywhere else.



          pre_delete can be used as a sanity check if you have objects that are shared in some way that wouldn't be caught by the more restrictive on_delete arguments. For example, if a teacher should only be allowed to delete a student object if that object has no associated attendance records, this could be one place to enforce that.



          post_delete, aside from things like statistical recalculations, can be used to alert admins or moderators to important user behavior. If a subscriber deletes an active payment method, in addition to deactivating that payment method on the payment processor, this signal could trigger an email to the relevant salesperson that they're in imminent danger of churn.



          When you get to signals, I'd say you're moving slightly out of the simple CRUD infrastructure that's probably Django's most common use case. So it's hard to point to the "most common use cases," as signals tend to become more useful as your project becomes more specialized.



          They're convenient when you need them, but personally, I only end up using them a few times a year; they're not something I'd suggest jumping for as a first option. Many or most things you'll need to do with Django can be done more simply and readably in, for example, an overridden save(...) method.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53264522%2fwhat-are-the-typical-use-cases-for-pre-save-post-save-pre-delete-post-delete%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            A very common use for pre_save is slugifying titles, e.g. on books, articles, or products. This is also one reason it's always better to keep a separate ID to key on in URLs, etc. rather than relying solely on slugs, otherwise you either can't change your slugs or need to work around the fact that persistent pages may not have persistent URLs, or have to support legacy URLs/redirects.



            post_save may also be used for firing off or enqueuing asynchronous events; if you keep your users synced with a separate service, for example, a successful change to the local user profile may need to be submitted to an API, an operation you wouldn't want to make the user wait for. This kind of thing could technically be done elsewhere, but putting it in this signal means you don't have to worry about implementing or invoking it anywhere else.



            pre_delete can be used as a sanity check if you have objects that are shared in some way that wouldn't be caught by the more restrictive on_delete arguments. For example, if a teacher should only be allowed to delete a student object if that object has no associated attendance records, this could be one place to enforce that.



            post_delete, aside from things like statistical recalculations, can be used to alert admins or moderators to important user behavior. If a subscriber deletes an active payment method, in addition to deactivating that payment method on the payment processor, this signal could trigger an email to the relevant salesperson that they're in imminent danger of churn.



            When you get to signals, I'd say you're moving slightly out of the simple CRUD infrastructure that's probably Django's most common use case. So it's hard to point to the "most common use cases," as signals tend to become more useful as your project becomes more specialized.



            They're convenient when you need them, but personally, I only end up using them a few times a year; they're not something I'd suggest jumping for as a first option. Many or most things you'll need to do with Django can be done more simply and readably in, for example, an overridden save(...) method.






            share|improve this answer


























              2














              A very common use for pre_save is slugifying titles, e.g. on books, articles, or products. This is also one reason it's always better to keep a separate ID to key on in URLs, etc. rather than relying solely on slugs, otherwise you either can't change your slugs or need to work around the fact that persistent pages may not have persistent URLs, or have to support legacy URLs/redirects.



              post_save may also be used for firing off or enqueuing asynchronous events; if you keep your users synced with a separate service, for example, a successful change to the local user profile may need to be submitted to an API, an operation you wouldn't want to make the user wait for. This kind of thing could technically be done elsewhere, but putting it in this signal means you don't have to worry about implementing or invoking it anywhere else.



              pre_delete can be used as a sanity check if you have objects that are shared in some way that wouldn't be caught by the more restrictive on_delete arguments. For example, if a teacher should only be allowed to delete a student object if that object has no associated attendance records, this could be one place to enforce that.



              post_delete, aside from things like statistical recalculations, can be used to alert admins or moderators to important user behavior. If a subscriber deletes an active payment method, in addition to deactivating that payment method on the payment processor, this signal could trigger an email to the relevant salesperson that they're in imminent danger of churn.



              When you get to signals, I'd say you're moving slightly out of the simple CRUD infrastructure that's probably Django's most common use case. So it's hard to point to the "most common use cases," as signals tend to become more useful as your project becomes more specialized.



              They're convenient when you need them, but personally, I only end up using them a few times a year; they're not something I'd suggest jumping for as a first option. Many or most things you'll need to do with Django can be done more simply and readably in, for example, an overridden save(...) method.






              share|improve this answer
























                2












                2








                2






                A very common use for pre_save is slugifying titles, e.g. on books, articles, or products. This is also one reason it's always better to keep a separate ID to key on in URLs, etc. rather than relying solely on slugs, otherwise you either can't change your slugs or need to work around the fact that persistent pages may not have persistent URLs, or have to support legacy URLs/redirects.



                post_save may also be used for firing off or enqueuing asynchronous events; if you keep your users synced with a separate service, for example, a successful change to the local user profile may need to be submitted to an API, an operation you wouldn't want to make the user wait for. This kind of thing could technically be done elsewhere, but putting it in this signal means you don't have to worry about implementing or invoking it anywhere else.



                pre_delete can be used as a sanity check if you have objects that are shared in some way that wouldn't be caught by the more restrictive on_delete arguments. For example, if a teacher should only be allowed to delete a student object if that object has no associated attendance records, this could be one place to enforce that.



                post_delete, aside from things like statistical recalculations, can be used to alert admins or moderators to important user behavior. If a subscriber deletes an active payment method, in addition to deactivating that payment method on the payment processor, this signal could trigger an email to the relevant salesperson that they're in imminent danger of churn.



                When you get to signals, I'd say you're moving slightly out of the simple CRUD infrastructure that's probably Django's most common use case. So it's hard to point to the "most common use cases," as signals tend to become more useful as your project becomes more specialized.



                They're convenient when you need them, but personally, I only end up using them a few times a year; they're not something I'd suggest jumping for as a first option. Many or most things you'll need to do with Django can be done more simply and readably in, for example, an overridden save(...) method.






                share|improve this answer












                A very common use for pre_save is slugifying titles, e.g. on books, articles, or products. This is also one reason it's always better to keep a separate ID to key on in URLs, etc. rather than relying solely on slugs, otherwise you either can't change your slugs or need to work around the fact that persistent pages may not have persistent URLs, or have to support legacy URLs/redirects.



                post_save may also be used for firing off or enqueuing asynchronous events; if you keep your users synced with a separate service, for example, a successful change to the local user profile may need to be submitted to an API, an operation you wouldn't want to make the user wait for. This kind of thing could technically be done elsewhere, but putting it in this signal means you don't have to worry about implementing or invoking it anywhere else.



                pre_delete can be used as a sanity check if you have objects that are shared in some way that wouldn't be caught by the more restrictive on_delete arguments. For example, if a teacher should only be allowed to delete a student object if that object has no associated attendance records, this could be one place to enforce that.



                post_delete, aside from things like statistical recalculations, can be used to alert admins or moderators to important user behavior. If a subscriber deletes an active payment method, in addition to deactivating that payment method on the payment processor, this signal could trigger an email to the relevant salesperson that they're in imminent danger of churn.



                When you get to signals, I'd say you're moving slightly out of the simple CRUD infrastructure that's probably Django's most common use case. So it's hard to point to the "most common use cases," as signals tend to become more useful as your project becomes more specialized.



                They're convenient when you need them, but personally, I only end up using them a few times a year; they're not something I'd suggest jumping for as a first option. Many or most things you'll need to do with Django can be done more simply and readably in, for example, an overridden save(...) method.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 13 '18 at 6:08









                kungphu

                2,75111324




                2,75111324






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53264522%2fwhat-are-the-typical-use-cases-for-pre-save-post-save-pre-delete-post-delete%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    Xamarin.form Move up view when keyboard appear

                    Post-Redirect-Get with Spring WebFlux and Thymeleaf

                    Anylogic : not able to use stopDelay()