I want to define a setter function for a Django model field in order to validate the input and immediately raise a error if value is not valid.
I don’t think that doing the validation in save() or pre_save() is a feasible solution for my problem because of things beyond my control.
Also changing the field into a property and use plain field.getter and field.setter are not feasible as this will break .filter(), .exclude() etc on the field, plus it will require a scheme change even if I changed field to _field and overrode the manager.
So, am I forced to override __setattr__ or is there a better way of doing this?
See this django ticket:
https://code.djangoproject.com/ticket/3148
Also, see here for discussion:
Django model fields getter / setter
And this blog post:
http://www.b-list.org/weblog/2006/aug/18/django-tips-using-properties-models-and-managers/
Also note there is the option of using a custom manager that translates your
filterkwargsfor you, and using_namefor fields, and making your own@name.setter@name.getteretc.