Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

form clean method vs model clean method vs model field validator

Tags:

python

django

I have a model with the field facebook_link.

With the help of a regular expression I want to validate if this link is really a facebook link.

The user can enter the facebook link in a form.

Should I place my validation in the clean method of the form field,

in the clean method of the model field

or should I use a a custom validator

like image 621
Nepo Znat Avatar asked Jul 17 '26 05:07

Nepo Znat


1 Answers

If I read the documentation correctly, I think the answer to your question is none of the above. According to the validators documentation, a built-in RegexValidator is available for you to validate data using regular expressions. This is likely what you should use (don't reinvent the wheel).

Technically speaking, however, I think validators are the right place to put this kind of logic, precisely because they can be used either on forms or models. That re-usability alone makes them worthwhile.

like image 70
Jonah Bishop Avatar answered Jul 18 '26 19:07

Jonah Bishop



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!