Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django rest framework. raise_exception=True

I'm wondering, when should I use serializer.is_valid(raise_exception=True)? If I'm not implementing any custom validation, do I need to use the raise_exeption=True flag? What if my API doesn't raise ValidationErrors, is it a bad practice? if it is, then why is the default raise_exception=False? I'm just wondering if I should set this to True. Thanks for your advice.

like image 868
Alejandro Veintimilla Avatar asked Oct 31 '16 19:10

Alejandro Veintimilla


1 Answers

Usually when validating a serializer we do something like this

if not serializer.is_valid(): raise ValidationError(serializer.errors) restapi catch this exception and return 400 response with the provided errors in form of list or dictionary. A cleaner way for writing the code above is

serializer.is_valid(raise_exception=True)

80% of the time u will want to use raise_exception=True unless you need to handle serializer's errors in your code rather than simply telling the user his input is wrong.

like image 74
Ramast Avatar answered Oct 13 '22 22:10

Ramast