0

In django I tend to check for changes and call save only if needed. For example I have the following code.

    # self == some Model...

    save_required = False
    if self.pct_complete != pct_complete:
        self.pct_complete = pct_complete
        save_required = True
    if self.phase_pct_complete != phase_pct_complete:
        self.phase_pct_complete = phase_pct_complete
        save_required = True
    if self.is_eligible_for_next_phase != is_eligible_for_next_phase:
        self.is_eligible_for_next_phase = is_eligible_for_next_phase
        save_required = True
    if save_required:
        self.save()

This was based on the comment I read where Django does not appear to track "before" vs. "after" states of a model.

The question:

I using others apps don't see many people tracking states and saving only when needed. Is there a reason for this or am I being overly paranoid?

rh0dium
  • 6,811
  • 4
  • 46
  • 79
  • I think you're being overly paranoid. Computer time is incredibly cheap, but I did find this in the archives. It will explain a better way to do what you want. http://stackoverflow.com/questions/1355150/django-when-saving-how-can-you-check-if-a-field-has-changed – Jake Nov 26 '12 at 13:13
  • Thanks Jake - I appreciate the input! – rh0dium Nov 26 '12 at 15:32

1 Answers1

1

I don't see a real need to do this unless you have large amounts of data in your columns or a high latency database connection or some other corner case.

In any case this new feature may help neaten your code: https://docs.djangoproject.com/en/dev/releases/1.5/#support-for-saving-a-subset-of-model-s-fields

scytale
  • 12,346
  • 3
  • 32
  • 46