Figuring out what has changed at save()
Sorry for the unwieldy title, but nothing else strikes me at the moment. I have a blog app -- a version of basic.blog, actually. There's a field in the model called "status" with two options: "draft" and "public." What I want to do is trigger an action the first time (and /only/ the first time) a particular instance is saved as "public." So: Write a post in the admin, save as draft --> No action Edit the post some, save as draft again --> No action Edit more, publish --> Action triggered! Edit again --> No action. How would I go about this? My thought is to override save(), but I can't figure out how to inspect the instance at that point to determine if the status attribute has changed since it was created. I assume that this information must be in there somewhere, since it seems like you'd need it to generate the SQL statement. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Figuring out what has changed at save()
So you need to trigger an action the first time you save an object in to the database. -- Class A: #self.id is None whenever a particular instance has not been saved into a database #override save method in the object as follows def save(self) : firstime = false if ~self.id : firsttime = true super.save() #this code only fires only when it is being saved for the first time. if firstime : #Trigger an action whichevery you want. - Hope the above code helps you in resolving the problem. On Mar 6, 11:02 am, "hanks...@gmail.com" wrote: > Sorry for the unwieldy title, but nothing else strikes me at the > moment. > > I have a blog app -- a version of basic.blog, actually. There's a > field in the model called "status" with two options: "draft" and > "public." > > What I want to do is trigger an action the first time (and /only/ the > first time) a particular instance is saved as "public." > > So: > > Write a post in the admin, save as draft --> No action > Edit the post some, save as draft again --> No action > Edit more, publish --> Action triggered! > Edit again --> No action. > > How would I go about this? My thought is to override save(), but I > can't figure out how to inspect the instance at that point to > determine if the status attribute has changed since it was created. I > assume that this information must be in there somewhere, since it > seems like you'd need it to generate the SQL statement. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Figuring out what has changed at save()
Rama: Thanks, but that's no good. That would trigger the action whether or not the post is saved as "draft." Hank Sims On Mar 6, 12:28 am, Rama Vadakattu wrote: > So you need to trigger an action the first time you save an object in > to the database. > > -- > > Class A: > >#self.id is None whenever a particular instance has not been saved > into a database > >#override save method in the object as follows > >def save(self) : >firstime = false > >if ~self.id : >firsttime = true > >super.save() > >#this code only fires only when it is being saved for the first > time. >if firstime : > #Trigger an action whichevery you want. > > - > > Hope the above code helps you in resolving the problem. > > On Mar 6, 11:02 am, "hanks...@gmail.com" wrote: > > > Sorry for the unwieldy title, but nothing else strikes me at the > > moment. > > > I have a blog app -- a version of basic.blog, actually. There's a > > field in the model called "status" with two options: "draft" and > > "public." > > > What I want to do is trigger an action the first time (and /only/ the > > first time) a particular instance is saved as "public." > > > So: > > > Write a post in the admin, save as draft --> No action > > Edit the post some, save as draft again --> No action > > Edit more, publish --> Action triggered! > > Edit again --> No action. > > > How would I go about this? My thought is to override save(), but I > > can't figure out how to inspect the instance at that point to > > determine if the status attribute has changed since it was created. I > > assume that this information must be in there somewhere, since it > > seems like you'd need it to generate the SQL statement. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Figuring out what has changed at save()
For what it's worth, I can imagine on way of doing this. I could use signals to monkeypatch the instance at post_init, adding an attribute called "post_init_status." Then I could compare this value with "status" at post_save. Please save me from doing something so ugly. On Mar 6, 12:28 am, Rama Vadakattu wrote: > So you need to trigger an action the first time you save an object in > to the database. > > -- > > Class A: > >#self.id is None whenever a particular instance has not been saved > into a database > >#override save method in the object as follows > >def save(self) : >firstime = false > >if ~self.id : >firsttime = true > >super.save() > >#this code only fires only when it is being saved for the first > time. >if firstime : > #Trigger an action whichevery you want. > > - > > Hope the above code helps you in resolving the problem. > > On Mar 6, 11:02 am, "hanks...@gmail.com" wrote: > > > Sorry for the unwieldy title, but nothing else strikes me at the > > moment. > > > I have a blog app -- a version of basic.blog, actually. There's a > > field in the model called "status" with two options: "draft" and > > "public." > > > What I want to do is trigger an action the first time (and /only/ the > > first time) a particular instance is saved as "public." > > > So: > > > Write a post in the admin, save as draft --> No action > > Edit the post some, save as draft again --> No action > > Edit more, publish --> Action triggered! > > Edit again --> No action. > > > How would I go about this? My thought is to override save(), but I > > can't figure out how to inspect the instance at that point to > > determine if the status attribute has changed since it was created. I > > assume that this information must be in there somewhere, since it > > seems like you'd need it to generate the SQL statement. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Figuring out what has changed at save()
On Thu, 2009-03-05 at 22:02 -0800, hanks...@gmail.com wrote: > Sorry for the unwieldy title, but nothing else strikes me at the > moment. > > I have a blog app -- a version of basic.blog, actually. There's a > field in the model called "status" with two options: "draft" and > "public." > > What I want to do is trigger an action the first time (and /only/ the > first time) a particular instance is saved as "public." > > So: > > Write a post in the admin, save as draft --> No action > Edit the post some, save as draft again --> No action > Edit more, publish --> Action triggered! > Edit again --> No action. > > How would I go about this? My thought is to override save(), but I > can't figure out how to inspect the instance at that point to > determine if the status attribute has changed since it was created. If the object has a primary key value, assuming you don't set that manually yourself, then it already exists in the database. So fetch the existing values. def save(*args, **kwargs): if self.pk: old_version = self.model.objects.get(pk=self.pk) # Work out the changes between "self" and "old_version" (Before anybody starts complaining about the extra database hit, think about how relatively infrequently new posts are saved in the scheme of things. Even if it was at the rapid rate of once per minute, you wouldn't notice the extra read.) > I > assume that this information must be in there somewhere, since it > seems like you'd need it to generate the SQL statement. No. Django just checks to see if the record is already in the database. We can't make any assumptions based on the primary key value for all models, since people can assign to that attribute. However, if you know that *your* code doesn't assign to that attribute, which is a pretty normal case, you'll be able to use the pk test to know if you're saving or updating. One day (maybe 1.2 timeframe?!) we'll add at least optional change detection to models and only update changed columns. There's a couple of implementation things to be worked out there first, but it's on the medium-term feature list. Regards, Malcolm --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Figuring out what has changed at save()
Keep track of a variable which tells us the action user is currently doing like "draft,publish,edit "? the first time the user does a publish you need to trigger an action. details: How to know the current action of user? --- a) 1. savedraft ->URL1 --->status = "draft" save/edit--->URL2-->status="publish" (or) pass an additional parameter in the url &status="action" which tell what action a user is doing? c) Let us categorize his actions in to two 1) draft =>when he does save draft 2) publish => when he does save/edit d) Also lets have a field in model named status which tells the current status of a record. by default associate status "draft" to every record. status = models.Charfield(default="draft") e) now instead of save() lets call customesave(param status) for every object which you want to save (i am assuming you bound a database object to form whenever such record already exists in the database) def customsave(self,status) { if(self.status = ="draft" && curstatus == "draft") { self.save(); } elif (self.status == "draft" && curstatus == "publish") { self.status = "publish" self.save() Trigger the actions which you need to perform for the first save() } elif(self.status == "publish" && curstatus=="publish") #user is editing { self.save() } } Hope the above helps. Please let me know if you need more clarifications. On Mar 6, 5:18 pm, Malcolm Tredinnick wrote: > On Thu, 2009-03-05 at 22:02 -0800, hanks...@gmail.com wrote: > > Sorry for the unwieldy title, but nothing else strikes me at the > > moment. > > > I have a blog app -- a version of basic.blog, actually. There's a > > field in the model called "status" with two options: "draft" and > > "public." > > > What I want to do is trigger an action the first time (and /only/ the > > first time) a particular instance is saved as "public." > > > So: > > > Write a post in the admin, save as draft --> No action > > Edit the post some, save as draft again --> No action > > Edit more, publish --> Action triggered! > > Edit again --> No action. > > > How would I go about this? My thought is to override save(), but I > > can't figure out how to inspect the instance at that point to > > determine if the status attribute has changed since it was created. > > If the object has a primary key value, assuming you don't set that > manually yourself, then it already exists in the database. So fetch the > existing values. > > def save(*args, **kwargs): > if self.pk: > old_version = self.model.objects.get(pk=self.pk) > # Work out the changes between "self" and "old_version" > > (Before anybody starts complaining about the extra database hit, think > about how relatively infrequently new posts are saved in the scheme of > things. Even if it was at the rapid rate of once per minute, you > wouldn't notice the extra read.) > > > I > > assume that this information must be in there somewhere, since it > > seems like you'd need it to generate the SQL statement. > > No. Django just checks to see if the record is already in the database. > We can't make any assumptions based on the primary key value for all > models, since people can assign to that attribute. However, if you know > that *your* code doesn't assign to that attribute, which is a pretty > normal case, you'll be able to use the pk test to know if you're saving > or updating. > > One day (maybe 1.2 timeframe?!) we'll add at least optional change > detection to models and only update changed columns. There's a couple of > implementation things to be worked out there first, but it's on the > medium-term feature list. > > Regards, > Malcolm --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Figuring out what has changed at save()
Keep track of a variable which tells us the action user is currently doing like "draft,publish,edit "? the first time the user does a publish you need to trigger an action. details: How to know the current action of user? --- a) 1. savedraft ->URL1 --->status = "draft" save/edit--->URL2-->status="publish" (or) pass an additional parameter in the url &status="action" which tell what action a user is doing? c) Let us categorize his actions in to two 1) draft =>when he does save draft 2) publish => when he does save/edit d) Also lets have a field in model named status which tells the current status of a record. by default associate status "draft" to every record. status = models.Charfield(default="draft") e) now instead of save() lets call customesave(param status) for every object which you want to save (i am assuming you bound a database object to form whenever such record already exists in the database) def customsave(self,status) { if(self.status = ="draft" && curstatus == "draft") { self.save(); } elif (self.status == "draft" && curstatus == "publish") { self.status = "publish" self.save() Trigger the actions which you need to perform for the first save() } elif(self.status == "publish" && curstatus=="publish") #user is editing { self.save() } } Hope the above helps. Please let me know if you need more clarifications. On Mar 6, 5:18 pm, Malcolm Tredinnick wrote: > On Thu, 2009-03-05 at 22:02 -0800, hanks...@gmail.com wrote: > > Sorry for the unwieldy title, but nothing else strikes me at the > > moment. > > > I have a blog app -- a version of basic.blog, actually. There's a > > field in the model called "status" with two options: "draft" and > > "public." > > > What I want to do is trigger an action the first time (and /only/ the > > first time) a particular instance is saved as "public." > > > So: > > > Write a post in the admin, save as draft --> No action > > Edit the post some, save as draft again --> No action > > Edit more, publish --> Action triggered! > > Edit again --> No action. > > > How would I go about this? My thought is to override save(), but I > > can't figure out how to inspect the instance at that point to > > determine if the status attribute has changed since it was created. > > If the object has a primary key value, assuming you don't set that > manually yourself, then it already exists in the database. So fetch the > existing values. > > def save(*args, **kwargs): > if self.pk: > old_version = self.model.objects.get(pk=self.pk) > # Work out the changes between "self" and "old_version" > > (Before anybody starts complaining about the extra database hit, think > about how relatively infrequently new posts are saved in the scheme of > things. Even if it was at the rapid rate of once per minute, you > wouldn't notice the extra read.) > > > I > > assume that this information must be in there somewhere, since it > > seems like you'd need it to generate the SQL statement. > > No. Django just checks to see if the record is already in the database. > We can't make any assumptions based on the primary key value for all > models, since people can assign to that attribute. However, if you know > that *your* code doesn't assign to that attribute, which is a pretty > normal case, you'll be able to use the pk test to know if you're saving > or updating. > > One day (maybe 1.2 timeframe?!) we'll add at least optional change > detection to models and only update changed columns. There's a couple of > implementation things to be worked out there first, but it's on the > medium-term feature list. > > Regards, > Malcolm --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---