What are the typical use cases for pre-save, post-save, pre-delete, post-delete signals in Django?
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
add a comment |
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
add a comment |
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
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
django django-signals
asked Nov 12 '18 at 14:42
darkhorse
1,42541845
1,42541845
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 13 '18 at 6:08
kungphu
2,75111324
2,75111324
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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