Easy REST API data validation using Django Forms
If your Django application has a REST API and you’re not using Django REST framework (or a similar framework) then you’ll find yourself writing lots of code for validating incoming data. But there is a simple way to do that using Django Forms.
Django ModelForm
If your API matches a model then use Django ModelForm class.
Say you have following Item
class:
Then validating the data is simply done by checking if the form is valid:
Django Form
In case that your model doesn’t match API format then you can use a Django Form to validate the data. After that you can save the data manually into corresponding models.
The code for form has the same attributes as previous ModelForm:
And the code for validation is basically the same:
Then only thing missing here is saving data and senging the response data. But that’s different from case to case.