dominno / django-moderation

django-moderation is reusable application for Django framework, that allows to moderate any model objects.
BSD 3-Clause "New" or "Revised" License
269 stars 91 forks source link

Modified content of object erased/reverted after calling my_object.save() #60

Open daniel2013 opened 12 years ago

daniel2013 commented 12 years ago

Hi,

I am using te following code to moderate my object:

class NodeModerator(GenericModerator):
    auto_approve_for_superusers = True
    auto_approve_for_staff = True

    visible_until_rejected = False

    visibility_column = 'visible_moderator'

    notify_user = False

    fields_exclude = ['hits', 'author', (and some more fields)] 
moderation.register(Node, NodeModerator)

Then I modify my object and the changes appear in the moderated object admin page.

But when I do:

my_object.author.hits += 1
my_object.author.save()

my_object.hits += 1
my_object.save()

All changes in the moderated object admin disappear/revert back.

Am I doing something wrong or this a bug? And how can I fix it?

Thanks.

treyhunner commented 12 years ago

This sounds like it might be a bug with the implementation of fields_exclude. Does moderation seem to work as expected when fields_exclude is not present?

daniel2013 commented 12 years ago

When commenting out fields_exclude the following happens:

daniel2013 commented 12 years ago

Do you think you can fix this bug anytime soon?

treyhunner commented 12 years ago

I am not currently using django-moderation actively, so I may not look into this soon. Sorry.

I would make sure you are modifying the "unmoderated" object (the unapproved object in the moderation queue) when applying your changes rather than the "moderated" object (the approved object that does not have changes already made to it). Example 1 (this might be what's happening in your case):

  1. You change object a
  2. You later query for a and change a again in a different way
  3. Your previous changes to a will be overwritten and replaced by the new ones (because you were modifying the already approved version of a instead of the unmoderated version)

Example 2:

  1. You change a
  2. You later query for a and change a.moderated_object.changed_object (this is the latest version of a which may not be moderated yet)
  3. Your initial changes to a and your second changes are both queued for moderation (and are now in a.moderated_object.changed_object)

If you're making your changes through a form, you can use a BaseModeratedObjectForm to make sure you're changing the unmoderated version of the object (read the "Forms" section of the README file).