← Back to Journal

Restless for building Python RESTful API

Posted on 28 November 2014

After trying to build RESTful API in Django with Django Rest Framework, my friend Pabluk allow me to discover Restless. A very small code base Python module to build RESTful API.

I’m totally not a huge fan of Django Rest Framework. It’s just to restricting if you don’t make a 100% REST API, and this is often the case. First of all, you’ll have a long progression period when you’ll have a bad use of this framework. I think it’s to complex while we try a build something simple. A RESTful API.

But, this is Restless. It has so small code base that it can fit to many web frameworks, like Flask, Pyramid, Django, etc…

Let’s try a Django integration.

Example

This is your existing models.py:

You have to understand that your API will expose Resources, this is how many framework call it. Let’s create a resources.py.

And now just expose it through your urls.py:

This is quite simple, I won’t explain you how to use POST, PATCH, etc… Just read documentation.

But just leave me show you a simple mixin for pagination. Let’s create a mixins.py, just as Django Class Based View.

You just have to update your resources.py:

And just enjoy your paginated Restful API. This is why I love Restless. I agree to say that you’ll write probably more code than Django Rest Framework but you’ll get more control of all the magic your Restless API do.

Restless is very simple, modular and not restrictive. It’s very close to KISS.

Another thing that I like, it’s the easiness to read Restless source code like Django Class Based Views. Simple, re-usable.

Simple is better than complex.