0

I've been stucked on this problem for a few days. How can I perform a specific action when an object's status has been updated?

I'v got an Order django model with a status field. When an order's status goes from new to processed, I need to perform some specific actions.

The thing is, the status can be updated from anywhere: a frontend view, an admin module, a bulk action, a management command, etc.

Also, I only want the action to be performed after the order is saved.

It seems the post_save signal could work, but I cannot find a way to detect if the status has changed, and what the old value is.

I checked the django fsm, but it's clearly not the way to go.

Any idea?

Thibault J
  • 4,336
  • 33
  • 44
  • Did you take a look at https://github.com/Atomidata/django-audit-log ? – arie Feb 06 '12 at 13:54
  • Seems a bit overkill. Plus « …it can only track changes to model instances when they are made via the web interface of your application. » – Thibault J Feb 06 '12 at 14:56

1 Answers1

1

To detect which fields have changed in the post_save signal, you somehow need to remember the original state of the model instance. One answer to Actions triggered by field change in Django is considering this and points to Dirty fields in django, where you can find various mix-ins that will allow you to find the "dirty" fields in your model when saving.

Community
  • 1
  • 1
Jan Pöschko
  • 5,412
  • 1
  • 28
  • 28
  • Yes, but as far as I understand, there is not instance created in the case of a bulk update, hence the dirty fields strategy won't work, right? – Thibault J Feb 06 '12 at 14:31
  • That's true, `update` doesn't call `save` nor trigger signals (https://docs.djangoproject.com/en/dev/topics/db/queries/#updating-multiple-objects-at-once). You could either overwrite `update` in a custom manager to do what you want, or handle updates with SQL directly, e.g. triggers: http://dev.mysql.com/doc/refman/5.0/en/triggers.html (not involving Django/Python at all). – Jan Pöschko Feb 06 '12 at 17:26