django/django — pull request #648 by MartinLindsay (2013-01-14 22:10:25+00:00)
Title: Added the ability override a setter for custom model fields

This adds a minimal __setattr__() to the base model that checks that property being set is in the field list and has a setter() method on that field. Then the value is run through the setter() method before actually assigning it. This gives the ability to actually create a setter for a custom model field that can transform or verify data as desired. See the changes that I made in the docs for full details and limitations. The unit test added demonstrates a simple example.

The main reason that I added this is because while using get_prep_value() to do the data transformations will change what's in the database, you need to refetch the object from the database to actually update the object that was originally created. This allows you to enforce changes on the value as soon as it is assigned, instead of only making changes for what is stored and retrieved.