pre_save vs post_save

138 views
Skip to first unread message

Alireza Savand

unread,
May 12, 2012, 5:46:08 AM5/12/12
to django...@googlegroups.com
Hi

What is the different between pre_save signal and post_save singal.
I know post_save will emited after model instance saved, and pre_save is before the saving the instance[?]
but what it the point of pre_save when we cannot access the object data before the data changes.
I mean user try to change the model content, like body of a post in a blog. using pre_save signal would be reasonable if i can access the post's body before the changes or in other word before the actual save() happens.
If i can't access to data before save() happends, So what is the point of pre_save ?

Karl Sutt

unread,
May 12, 2012, 6:32:15 AM5/12/12
to django...@googlegroups.com
Regarding pre_save signal, it is useful for doing something with the "old" instance. Consider a use case where you might want to "archive" all changes to, say, the user's profile. Every time a user makes a change to their profile, you want to save the current state of the profile to another table, or another database, and then save the new model (which overwrites the current state and becomes the new current state). This sort of mechanism is useful for versioning your model instances. The pre_save signal allows you to create a hook that does precisely that. That is just one of the use cases, but I'm sure you can think of others.

Karl Sutt


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/NLEgGtpvQ4cJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Alireza Savand

unread,
May 12, 2012, 6:38:08 AM5/12/12
to django...@googlegroups.com
Thanks for your answer Karl
Actually yes, i want to do some versioning on model instance, but the problem is that the state of the model instance in pre_save is same as at the post_save.

Karl Sutt

unread,
May 12, 2012, 6:44:08 AM5/12/12
to django...@googlegroups.com
Could you provide some code (paste it to dpaste.org for example)? Specifically, it would be useful to see your model definition and signal connectors etc.

Karl Sutt

Alireza Savand

unread,
May 12, 2012, 7:00:02 AM5/12/12
to django...@googlegroups.com
Okay
here the paste
And the Model is just Poll model from the tutorial on the django official document.

Karl Sutt

unread,
May 12, 2012, 7:13:28 AM5/12/12
to django...@googlegroups.com
Thanks. So, in onPreSave, you use kwargs['instance'], assuming it's the instance before saving (which it is, not just in the "current" state). So kwargs['instance'] in both handlers have the same state. If you want to get the current state (the old state, so to speak), you should to something like

sender.objects.get(id=kwargs['instance'].id)

This would get you the current state of the that instance.

Hope this is clear and helps.

Karl Sutt

Alireza Savand

unread,
May 12, 2012, 7:35:38 AM5/12/12
to django...@googlegroups.com
Thank you very much Karl, it works!

Karl Sutt

unread,
May 12, 2012, 7:50:07 AM5/12/12
to django...@googlegroups.com
Awesome! No problem.

Karl Sutt

Alireza Savand

unread,
May 13, 2012, 9:39:34 AM5/13/12
to django...@googlegroups.com
Okay, i found a problem in it.
One of my models doesn't use id as a default pk.
is use different primary key, called iso.
So here the traceback
It's not working with those model with different primary key :|

Karl Sutt

unread,
May 13, 2012, 10:13:42 AM5/13/12
to django...@googlegroups.com
I think you can just change the "id" in 

instance = sender.objects.get(id=kwargs['instance'].id)
 
to "iso" and that should to the trick. Try it, as I haven't verified it. And let us know.

Good luck,

Karl Sutt


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/b7HViV6o7AoJ.

Alireza Savand

unread,
May 13, 2012, 10:20:09 AM5/13/12
to django...@googlegroups.com
I tried already, another error will be issued ;), and it's totally weird
pasted at 

Exception Value: Cannot resolve keyword 'id' into field

Alireza Savand

unread,
May 13, 2012, 10:26:56 AM5/13/12
to django...@googlegroups.com
Aha i solved it

sender.objects.get(id=kwargs['instance'].id)
be 
sender.objects.get(pk=kwargs['instance'].pk)

Thank you again Karl ;)

Karl Sutt

unread,
May 13, 2012, 10:31:23 AM5/13/12
to django...@googlegroups.com
Ahh, great! No problem.

Karl Sutt


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/KQUNwR9k25sJ.
Reply all
Reply to author
Forward
0 new messages