How to raise ValidationError in Serializer.create() without losing form data?

454 views
Skip to first unread message

Max Arnold

unread,
Oct 30, 2017, 12:06:39 PM10/30/17
to Django REST framework
Hi folks!

Browsable DRF forms (and Django forms in general) usually keep the data after validation errors (they send the data back to the browser for correction). In DRF 3.x this only happens if exceptions.ValidationError is raised inside Serializer.is_valid(raise_exception=True) call.

In my case, some of the validation exceptions are raised in Serializer.create(), because I want DB row locking and transaction isolation. Unfortunately, this discards form data from the response, because serializer is not added to ReturnDict or ReturnList and BrowsableAPIRenderer.get_rendered_html_form() can't reuse it.

What is the best way to keep form data between validation errors and raise exceptions during Serializer.create()?  In pre 2.0 DRF days it was possible to raise validation exceptions inside view code without any problems.

Max Arnold

unread,
Oct 30, 2017, 12:38:43 PM10/30/17
to Django REST framework
Reply all
Reply to author
Forward
0 new messages