Django API Forms¶
Django Forms approach in the processing of a RESTful HTTP request payload (especially for content type like JSON or MessagePack) without HTML front-end.
Motivation¶
The main idea was to create a simple and declarative way to specify the format of expecting requests with the ability to validate them. Firstly, I tried to use Django Forms to validate my API requests (I use pure Django in my APIs). I have encountered a problem with nesting my requests without a huge boilerplate. Also, the whole HTML thing was pretty useless in my RESTful APIs.
I wanted to:
- define my requests as object (
Form
), - pass the request to my defined object (
form = Form.create_from_request(request, param=param))
), - you can also pass any extra optional arguments
- validate my request
form.is_valid()
, - extract data
form.clean_data
property.
I wanted to keep:
- friendly declarative Django syntax, (DeclarativeFieldsMetaclass is beautiful),
- Validators,
- ValidationError,
- Form fields (In the end, I had to "replace" some of them).
So I have decided to create a simple Python package to cover all my expectations.