We want to be able to consume Models and produce Models.
In essence, imagine you want an apple, and I pass you an apple. Then, when the apple reaches you, you get a bunch of random shit describing the apple, like "RED", appleCount: 1. This is pretty useless to us, because you wanted an apple and I just gave you a bunch of crap that describes the apple.
The serializer is all of the stuff that describes the apple, and Swagger is the tool that will allow you to get an actual apple from the serializer.
In reality, the backend will serve serialized objects, and generate this cool file called "swagger.json" which the front end will generate a bunch of code from to turn serialized objects into real JavaScript objects, effectively turning our backend Python Models to Javascript Models.
Further, Swagger lets us call the backend easier. Right now, if I want to call the backend I will have to make some HTTP request like curl 127.0.0.1:3000/Session blah blah blah
. And in JavaScript code, I'd have to do the same thing but with JavaScript.
However, Swagger will create our own API for us, so if I want to GET something, I can do something like:
Api.View.Get()
on the JavaScript side.
"swagger.json" is generated through us basically writing comments (literally writing comments if you want do, but I like the decorator format more). We can call the generator like this:
python manage.py generate_swagger swagger.json
which goes through every single python file and creates the Swagger Api for our front end to consume.
So, our goal is to serialize all Django Models, and generate the swagger file.
- Add https://drf-yasg.readthedocs.io/en/stable/readme.html to the
requirements.txt
- Serialize every model, and make sure to include the
Meta
class inside the models (its for swagger)
- On every instance of us doing a
GET
call, serialize the output THEN return.
- On every instance of us doing a 'DELETE` call, take in an object, deserialize it
- One every instance of us doing a
PUT/PATCH
call, take in an object, deserialize it
The beauty of the Django serializer is that everything is done for us, and all errors are handled by the serializer itself.