4

I wanted to do field validation on a django model without using ModelForms. Is there a way I can get the clean_fieldname method to be called when save() is invoked?

pg2286
  • 1,011
  • 12
  • 21
  • You could declare any custom form and write a clean method yourself. Then you can overwrite the Form save() method and make it save, delete, or do whatever you want to do. – PepperoniPizza Nov 14 '12 at 20:06

1 Answers1

2

The clean_fieldname method belongs on a form or a model form. There's no code in the model to do the same thing, you'd have to implement it yourself.

I recommend you write a validator for your field, then call full_clean() before saving to validate your instance.

Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • Thanks calling full_clean() will do the work. Is there a cleaner way like validators to write data manipulator get it automatically called on save() as well apart from full_clean()? – pg2286 Nov 14 '12 at 20:27
  • I've [answered](http://stackoverflow.com/questions/12945339/is-this-the-way-to-validate-django-model-fields/12945692#12945692) [similar](http://stackoverflow.com/questions/8771029/django-raise-a-validation-error-in-a-models-save-method) [questions](http://stackoverflow.com/questions/12608639/django-field-validation-in-model-and-in-admin) on stack overflow before, hopefully they'll help explain further. – Alasdair Nov 14 '12 at 21:37