Re: access the state of a Model object prior to it's modification ?
On Mon, Jul 30, 2012 at 6:22 PM, Tomas Nemewrote: > > uhm, if you register a listener to the pre_save signal, then you can do > this: > > > if instance.published: > # the instance about to be saved has the published flag on > dbojb = MyModel.objects.get(id=instance.id) > > # did it have it on the database? > if not dbobj.published: > do_stuff() > > you probably need to check the "created" argument on the listener that > tells you if the object is a new one, or a modification > > Great! ... this is what I was looking for. Signals is what I meant by "the django way of .." Thanks all! > -- > "The whole of Japan is pure invention. There is no such country, there are > no such people" --Oscar Wilde > > |_|0|_| > |_|_|0| > |0|0|0| > > (\__/) > (='.'=)This is Bunny. Copy and paste bunny > (")_(") to help him gain world domination. > > -- > 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. > -- Nicolas Emiliani Lo unico instantaneo en la vida es el cafe, y es bien feo. -- 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: access the state of a Model object prior to it's modification ?
uhm, if you register a listener to the pre_save signal, then you can do this: if instance.published: # the instance about to be saved has the published flag on dbojb = MyModel.objects.get(id=instance.id) # did it have it on the database? if not dbobj.published: do_stuff() you probably need to check the "created" argument on the listener that tells you if the object is a new one, or a modification -- "The whole of Japan is pure invention. There is no such country, there are no such people" --Oscar Wilde |_|0|_| |_|_|0| |0|0|0| (\__/) (='.'=)This is Bunny. Copy and paste bunny (")_(") to help him gain world domination. -- 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: access the state of a Model object prior to it's modification ?
This is a long shot but try checking out django-reversion > Sounds interesting. > thank you. > > -- > 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/-/NM39pkUyvG4J. > > 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. > -- Nicolas Emiliani Lo unico instantaneo en la vida es el cafe, y es bien feo. -- 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: access the state of a Model object prior to it's modification ?
Hello, This is a long shot but try checking out django-reversion thank you. -- 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/-/NM39pkUyvG4J. 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: access the state of a Model object prior to it's modification ?
On 15-7-2012 0:49, Nicolas Emiliani wrote: > obj.published == True > > Is there a way to know which was the state of the model, in this case the > state of obj.published, > before the user clicked on the save button on the admin form ? Set the form field of the ModelAdmin to a custom form that derives forms.ModelForm. Add a BooleanField to this custom form with a HiddenInput widget. Populate that field in ModelAdmin.get_form(). Some wrestling with formfield_overrides may be necessary. -- Melvyn Sopacua -- 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: access the state of a Model object prior to it's modification ?
Jani, thanks for the idea, some thoughts below your code. > > class MyPublicationModel(Model): > def save(...): # I don't remember the signature > if self.is_published and not self.publication_date: > # Published previously unpublished - set the date > self.publication_date = date.today() >elif not self.is_published and self.publication_date: > # Mark already published as unpublished - clear the date > self.publication_date = None > >super(self, MyPublicationModel).save(...) > > Yes, you are right, but you are clearing the date, the thing is that I don't want to clear it. That's why I thought of having an attribute in the model that does not render in the form, that would allow me to keep the previous state unchanged and use that in my logic. > On Mon, Jul 16, 2012 at 3:39 AM, Nicolas Emilianiwrote: > >> >> >> On Sun, Jul 15, 2012 at 8:10 PM, Jani Tiainen wrote: >> >>> Hi, >>> >>> How about telling us what are you trying to achieve by comparing to >>> previous state of the object? >>> >>> >> Well, the model has a boolean field called published, and a publish_date >> that gets set "automatically" >> when the user through the admin panel sets the flag published to true. He >> could also unpublish it, but the >> date would still remain the same (on the date it was first published). >> And here comes the problem : >> >> If the user decides to republish I would have to set the date to current >> date, but how do I know if he >> is republishing or he modified some other field that has nothing to do >> with that attribute ? I would be >> republishing only if it's previous state was not published. But how can I >> acces that previous state ? >> >> Sounds waky :P >> >> >> >>> On Sun, Jul 15, 2012 at 1:49 AM, Nicolas Emiliani wrote: >>> Hi, Is there a way to access the state of a Model object prior to it's modification through a form ? Kind of a nasty question :S, let me explain. The thing is that if i use the save_model hook and the user modifies the model through the form, the obj parameter that I receive has already all the modifications loaded, so if I have, let's say, a boolean attribute called "published" and the user clicked published then : obj.published == True Is there a way to know which was the state of the model, in this case the state of obj.published, before the user clicked on the save button on the admin form ? or should I use a second model attribute and hide it on the form to keep the previous state ? is there a "Django" way to do this ? It's my first post, be gentle :P Thanks ! -- Nicolas Emiliani Lo unico instantaneo en la vida es el cafe, y es bien feo. -- 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. >>> >>> >>> >>> -- >>> Jani Tiainen >>> >>> - Well planned is half done, and a half done has been sufficient >>> before... >>> >>> -- >>> 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. >>> >> >> >> >> -- >> Nicolas Emiliani >> >> Lo unico instantaneo en la vida es el cafe, y es bien feo. >> >> -- >> 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. >> > > > > -- > Jani Tiainen > > - Well planned is half done, and a half done has been sufficient before... > > -- > 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. > -- Nicolas Emiliani Lo unico instantaneo en la vida es el cafe, y es bien feo. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to
Re: access the state of a Model object prior to it's modification ?
Probably you want to do that logic a slightly differently on a model level (so rule would apply always when you save your model). So you would actually override model save method. Probably rule is something like this: class MyPublicationModel(Model): def save(...): # I don't remember the signature if self.is_published and not self.publication_date: # Published previously unpublished - set the date self.publication_date = date.today() elif not self.is_published and self.publication_date: # Mark already published as unpublished - clear the date self.publication_date = None super(self, MyPublicationModel).save(...) On Mon, Jul 16, 2012 at 3:39 AM, Nicolas Emilianiwrote: > > > On Sun, Jul 15, 2012 at 8:10 PM, Jani Tiainen wrote: > >> Hi, >> >> How about telling us what are you trying to achieve by comparing to >> previous state of the object? >> >> > Well, the model has a boolean field called published, and a publish_date > that gets set "automatically" > when the user through the admin panel sets the flag published to true. He > could also unpublish it, but the > date would still remain the same (on the date it was first published). And > here comes the problem : > > If the user decides to republish I would have to set the date to current > date, but how do I know if he > is republishing or he modified some other field that has nothing to do > with that attribute ? I would be > republishing only if it's previous state was not published. But how can I > acces that previous state ? > > Sounds waky :P > > > >> On Sun, Jul 15, 2012 at 1:49 AM, Nicolas Emiliani wrote: >> >>> Hi, >>> >>> Is there a way to access the state of a Model object prior to it's >>> modification through a form ? >>> Kind of a nasty question :S, let me explain. >>> >>> The thing is that if i use the save_model hook and the user modifies the >>> model through the form, >>> the obj parameter that I receive has already all the modifications >>> loaded, so if I have, let's say, >>> a boolean attribute called "published" and the user clicked published >>> then : >>> >>> obj.published == True >>> >>> Is there a way to know which was the state of the model, in this case >>> the state of obj.published, >>> before the user clicked on the save button on the admin form ? or should >>> I use a second model >>> attribute and hide it on the form to keep the previous state ? is there >>> a "Django" way to do this ? >>> >>> It's my first post, be gentle :P >>> >>> Thanks ! >>> >>> -- >>> Nicolas Emiliani >>> >>> Lo unico instantaneo en la vida es el cafe, y es bien feo. >>> >>> -- >>> 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. >>> >> >> >> >> -- >> Jani Tiainen >> >> - Well planned is half done, and a half done has been sufficient before... >> >> -- >> 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. >> > > > > -- > Nicolas Emiliani > > Lo unico instantaneo en la vida es el cafe, y es bien feo. > > -- > 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. > -- Jani Tiainen - Well planned is half done, and a half done has been sufficient before... -- 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: access the state of a Model object prior to it's modification ?
On Sun, Jul 15, 2012 at 8:10 PM, Jani Tiainenwrote: > Hi, > > How about telling us what are you trying to achieve by comparing to > previous state of the object? > > Well, the model has a boolean field called published, and a publish_date that gets set "automatically" when the user through the admin panel sets the flag published to true. He could also unpublish it, but the date would still remain the same (on the date it was first published). And here comes the problem : If the user decides to republish I would have to set the date to current date, but how do I know if he is republishing or he modified some other field that has nothing to do with that attribute ? I would be republishing only if it's previous state was not published. But how can I acces that previous state ? Sounds waky :P > On Sun, Jul 15, 2012 at 1:49 AM, Nicolas Emiliani wrote: > >> Hi, >> >> Is there a way to access the state of a Model object prior to it's >> modification through a form ? >> Kind of a nasty question :S, let me explain. >> >> The thing is that if i use the save_model hook and the user modifies the >> model through the form, >> the obj parameter that I receive has already all the modifications >> loaded, so if I have, let's say, >> a boolean attribute called "published" and the user clicked published >> then : >> >> obj.published == True >> >> Is there a way to know which was the state of the model, in this case the >> state of obj.published, >> before the user clicked on the save button on the admin form ? or should >> I use a second model >> attribute and hide it on the form to keep the previous state ? is there a >> "Django" way to do this ? >> >> It's my first post, be gentle :P >> >> Thanks ! >> >> -- >> Nicolas Emiliani >> >> Lo unico instantaneo en la vida es el cafe, y es bien feo. >> >> -- >> 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. >> > > > > -- > Jani Tiainen > > - Well planned is half done, and a half done has been sufficient before... > > -- > 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. > -- Nicolas Emiliani Lo unico instantaneo en la vida es el cafe, y es bien feo. -- 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: access the state of a Model object prior to it's modification ?
Hi, How about telling us what are you trying to achieve by comparing to previous state of the object? On Sun, Jul 15, 2012 at 1:49 AM, Nicolas Emilianiwrote: > Hi, > > Is there a way to access the state of a Model object prior to it's > modification through a form ? > Kind of a nasty question :S, let me explain. > > The thing is that if i use the save_model hook and the user modifies the > model through the form, > the obj parameter that I receive has already all the modifications loaded, > so if I have, let's say, > a boolean attribute called "published" and the user clicked published > then : > > obj.published == True > > Is there a way to know which was the state of the model, in this case the > state of obj.published, > before the user clicked on the save button on the admin form ? or should I > use a second model > attribute and hide it on the form to keep the previous state ? is there a > "Django" way to do this ? > > It's my first post, be gentle :P > > Thanks ! > > -- > Nicolas Emiliani > > Lo unico instantaneo en la vida es el cafe, y es bien feo. > > -- > 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. > -- Jani Tiainen - Well planned is half done, and a half done has been sufficient before... -- 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.
access the state of a Model object prior to it's modification ?
Hi, Is there a way to access the state of a Model object prior to it's modification through a form ? Kind of a nasty question :S, let me explain. The thing is that if i use the save_model hook and the user modifies the model through the form, the obj parameter that I receive has already all the modifications loaded, so if I have, let's say, a boolean attribute called "published" and the user clicked published then : obj.published == True Is there a way to know which was the state of the model, in this case the state of obj.published, before the user clicked on the save button on the admin form ? or should I use a second model attribute and hide it on the form to keep the previous state ? is there a "Django" way to do this ? It's my first post, be gentle :P Thanks ! -- Nicolas Emiliani Lo unico instantaneo en la vida es el cafe, y es bien feo. -- 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.