I throw ValidationError while validating data in my clean method in models.py. How can I catch this error in my custom create method so that it throws a json object containing the error detail in this
way
{
"detail":"input is not valid"
}
#models.py
class Comment(models.Model):
text = models.CharField(max_length=256)
commenter = models.ForeignKey(User, on_delete=models.SET_NULL)
post = models.ForeignKey(Post, on_delete=models.SET_NULL)
def clean(self, *args, **kwargs):
if containsBadWords(text):
raise ValidationError(_("Be Polite"))
#serializer.py
def create(self, validated_data):
request = self.context.get('request', None)
commenter = request.user
try:
obj = Comment.objects.create(
post = validated_data['post'],
commenter = commenter,
text = validated_data['text']
)
except ValidationError as ex:
raise ex
return obj