access the state of a Model object prior to it's modification ?

33 views
Skip to first unread message

Nicolas Emiliani

unread,
Jul 14, 2012, 6:49:51 PM7/14/12
to django...@googlegroups.com
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.

Jani Tiainen

unread,
Jul 15, 2012, 7:10:25 PM7/15/12
to django...@googlegroups.com
Hi,

How about telling us what are you trying to achieve by comparing to previous state of the object?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
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.



--
Jani Tiainen

- Well planned is half done, and a half done has been sufficient before...

Nicolas Emiliani

unread,
Jul 15, 2012, 8:39:31 PM7/15/12
to django...@googlegroups.com
On Sun, Jul 15, 2012 at 8:10 PM, Jani Tiainen <red...@gmail.com> 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

Jani Tiainen

unread,
Jul 16, 2012, 4:08:26 AM7/16/12
to django...@googlegroups.com
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(...)

Nicolas Emiliani

unread,
Jul 16, 2012, 8:27:20 AM7/16/12
to django...@googlegroups.com
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. 

Melvyn Sopacua

unread,
Jul 30, 2012, 8:35:19 AM7/30/12
to django...@googlegroups.com
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

megaBos

unread,
Jul 30, 2012, 10:10:34 AM7/30/12
to django...@googlegroups.com
Hello,

This is a long shot but try checking out django-reversion

thank you.

Nicolas Emiliani

unread,
Jul 30, 2012, 4:34:55 PM7/30/12
to django...@googlegroups.com
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...@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.

Tomas Neme

unread,
Jul 30, 2012, 5:22:25 PM7/30/12
to django...@googlegroups.com

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.

Nicolas Emiliani

unread,
Jul 31, 2012, 8:38:43 AM7/31/12
to django...@googlegroups.com
On Mon, Jul 30, 2012 at 6:22 PM, Tomas Neme <lacry...@gmail.com> wrote:

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...@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.
Reply all
Reply to author
Forward
0 new messages