🐝 Django REST Framework 🐝
Posted on Jul 15th, 2021
Today’s Topics
Today we are going to dive into the Django REST Framework.
🎯 Project
🎯 Option 1: Add an API to Habit Tracker
Add a new app to your existing Habit Tracker project and call it api
. Your app should provide CRUD endpoints that return JSON responses.
By Friday afternoon, you should be able to list, create, update, and delete habits via the API.
By Monday, you should be able to complete all CRUD tasks through the API:
- list habits
- create a new habit
- view a habit
- update a habit
- delete a habit
- list records for a habit (this should be on the habit detail API endpoint)
- create a record for a habit
- update a record for a habit
- delete a record from a habit
Write up a list of endpoints you think you will need before you start doing this!
You should develop and test your endpoints using Insomnia. (You can also use the browsable API that DRF gives you in the browser, but you should be comfortable using Insomnia.)
🎯 Option 2: Django Library API
Link to assignment invitation. Note, this is an empty repo, so you won’t have to delete a README to start.
Create a new API-only application that lets users keep track of their books, including important information like title, author, publication date, a field that marks it as “featured”, and maybe other fields like “genre” if you want. Books should be unique by title and author (that is, you can’t have two books with the same title and author; two books with the same title is fine as long as the authors are different).
Anyone can add a new book as long as the same book is not already in the library. Only admin users can update book details and delete books.
You’ll also need a book tracking model so that users can mark a book as “want to read”, “reading”, or “read/done”. Users can also write reviews on books, which should be viewable to all users. Optionally users can take private notes on books.
You should not make forms or templates for this app, just models, urls, views, and serializers. Your views should return JSON responses.
Your app should allow users to:
- list all books
- list all featured books
- create a book
- retrieve details about a book
- mark a book as want to read, reading, or read
- retrieve all reviews for a book
- post a review for a book
- edit their own review (not delete it)
Admin users can:
- update a book
- mark a book as featured
- delete a book
- delete reviews
🚀 Whichever option you choose, the application should be deployed to Heroku.
📖 Read | 📺 Watch | 🎧 Listen
- 📖 DRF class-based views This talks about
APIView
. - 📖 Class-based vs. function-based views This is about Django views in general, not specific to DRF.
- 📺 What You Should Know About DRF, Lacey Williams Henschel, PyCascades 2021
🔖 Resources
Blog articles that go with Lacey Williams Henschel’s talk
- What You Should Know About DRF, Part 1: ModelViewSet attributes and methods
- What You Should Know About DRF, Part 2: Customizing built-in methods
- What You Should Know About DRF, Part 3: Adding custom endpoints