Friday, May 7, 2010

What is the proper way to check the previous value of a field before saving an object? (Using Django)

Programmer Question


I have a Django Model with updated_by and an approved_by fields, both are ForeignKey fields to the built-in (auth) User models.



I am aware that with updated_by, it's easy enough to simply over-ride the .save() method on the Model, and shove the request.user in that field before saving.



However, for approved_by, this field should only ever be filled in when a related field (date_approved) is first filled in. I'm somewhat certain that I can check this logically, and fill in the field if the previous value was empty.



What is the proper way to check the previous value of a field before saving an object?



I do not anticipate that date_approved will ever be changed or updated, nor should there be any reason to ever update the approved_by entry.



UPDATE:

Regarding forms/validation, I should have mentioned that none of the fields in question are seen by or editable by users of the site. If I have misunderstood, I'm sorry, but I'm not sure how forms and validation apply to my question.





Find the answer here

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails