Skip to Content
Test-Driven Development with Python, 2nd Edition
book

Test-Driven Development with Python, 2nd Edition

by Harry Percival
August 2017
Intermediate to advanced
624 pages
12h 18m
English
O'Reilly Media, Inc.
Content preview from Test-Driven Development with Python, 2nd Edition

Appendix G. Django-Rest-Framework

Having “rolled our own” REST API in the last appendix, it’s time to take a look at Django-Rest-Framework, which is a go-to choice for many Python/Django developers building APIs. Just as Django aims to give you all the basic tools that you’ll need to build a database-driven website (an ORM, templates, and so on), so DRF aims to give you all the tools you need to build an API, and thus avoid you having to write boilerplate code over and over again.

Writing this appendix, one of the main things I struggled with was getting the exact same API that I’d just implemented manually to be replicated by DRF. Getting the same URL layout and the same JSON data structures I’d defined proved to be quite a challenge, and I felt like I was fighting the framework.

That’s always a warning sign. The people who built Django-Rest-Framework are a lot smarter than I am, and they’ve seen a lot more REST APIs than I have, and if they’re opinionated about the way that things “should” look, then maybe my time would be better spent seeing if I can adapt and work with their view of the world, rather than forcing my own preconceptions onto it.

“Don’t fight the framework” is one of the great pieces of advice I’ve heard. Either go with the flow, or perhaps reassess whether you want to be using a framework at all.

We’ll work from the API we had at the end of the last appendix, and see if we can rewrite it to use DRF.

Installation

A quick pip install gets us DRF. I’m just using ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Test-Driven Development with Python, 3rd Edition

Test-Driven Development with Python, 3rd Edition

Harry Percival

Publisher Resources

ISBN: 9781491958698Errata Page