Django REST Framework

The Dango Rest Framework (DRF) is the leading REST API library for Django projects. It has fantastic and comprehensive documentation, much like the Django project. However, with all the classes, mixins, and other options it provides, the abstractions can get a bit confusing.

Whenever I use a library I like to have a clear picture of its main concepts and how the pieces fit together, so I ended up plotting this out for the DRF to show how the URLs, REST operations, and CRUD layer all fit together to make an API. This is intended for someone who has read over the DRF documentation and feels like they kind of get it, but starts to feel lost in all the different layers of abstraction.

Behold, the Django REST Framework - Abstraction Grid:

Django REST Framework Abstraction Grid

(click to view full size: svg | pdf | png)

The big things to see here are:

  1. You are expected to follow their mostly flat structure of "/{your_objects}" or "/{your_objects}/{pk}" and to apply the various HTTP methods as shown—if you fight this structure, you will have a bad time
  2. The Generics are all doing the REST operation work (connecting to a URL and HTTP method)
  3. The Mixins are all doing the CRUD operations (connecting into the DB with your model)
  4. See if you can use GenericViews Combos or better the ViewSets
  5. This doesn’t include serializers, sorry!

If some of this is outdated, let me know! I have it in a sketch file that is easy to update.

Javascript lends itself really well to writing asynchronous code, but when you open up the JavaScript console in a web browser or the command-line interpreter in Node, it becomes annoying really fast to test out an asynchronous method and see what it returns.

The async/await pattern lends itself really well to writing code in a repl, and while you cannot use await in top-level JavaScript code, you can in the repl!

In the Chrome JavaScript console, you can just use await:

> await fetch('https://official-joke-api.herokuapp.com/random_joke')
    .then(r => r.json())

{id: 4, type: "general", setup: "What do you call a belt made out of watches?", punchline: "A waist of time."}

With Node 10+ you can do --experimental-repl-await (thanks vsemozhetbyt!):

$ node --experimental-repl-await
> await require('fs').promises.readFile('hi.txt', 'utf8')

'hi!\n'

You can also go ahead and set an alias for node to use this flag by default.

Enjoy!

An example of how to safely inline JSON within a script in a Jekyll template to avoid XSS attacks and to allow access to that data in your own JavaScript. read full post…
A very short example of how to pass a function into setState in React. read full post…
A convenience function for writing mixed color and/or font-style text using the HTML5 canvas, which is made possible by the ctx.measureText method. read full post…

Peter Coles

Peter Coles

is a software engineer who lives in NYC, is currently working on this, and blogging here. more »

github · soundcloud · @lethys · rss

It’s time to get big money out of politics. Join the kick-started campaign to put government back in the hands of the people. Pledge mayday.us now