Skip to content

Server side API management

Adrien Bocquet edited this page Sep 13, 2018 · 1 revision

Create server Side API

Create a model

Models are defined in a model.py if there are few. If your module can be split in several sub-modules, please make different files and include them in a model folder. The files will then be loaded in the __init__.pyfile. A good example of that is given throughout the "associations" module.

Create a serializer

A serializer converts models to JSON and vice-versa. They should be defined in a serializer folder or file, according to what's been said in the previous section. One can define different serializers for different uses, for example for a list view or a detail view.

Find more information at http://www.django-rest-framework.org/api-guide/serializers/

Create the view

The view gets the request, loads the models, serializes it and sends the response. It's pretty straightforward. Once more, please use the same file convention as before, and read the doc at : http://www.django-rest-framework.org/api-guide/views/

Register in the router

Last but not least, you need to add your view to the router, which is currently defined in api/urls.py. Maybe it should not include all the routes. Feel free to refactor it a little 🙂

Filtering

You can filter API results through GET parameters. For instance api/v1/rest/member?city=Gotham will return all the users whose city of origin is Gotham. You can use very powerful url filtering in the doc https://github.com/miki725/django-url-filter. The module is already installed, you only have to load it.