π» Views and Serializers in DRF π»
Posted on May 26th, 2022
ποΈ Todayβs Topics
- Views and viewsets
- Nesting and customizing serializers
- Permissions
π― Project
Keep on with your API building π πͺ! What do you need to know to get it working?
By now you should have a list of endpoints that your API offers, even if they are not all complete yet. A good place for this would be in your README, formatted with Markdown. This list should show the URLs along with the HTTP methods/verbs, and ideally should include an example of the JSON that has to be included in the request body (for any requests that send data in the body) and an example of the JSON response that will be returned, for each endpoint. This list will be helpful for your own testing and it can serve as necessary documentation for your API.
Here is an example of what your documentation might look like for an endpoint to create a book:
Create a new book
request
Requires authentication.
title
and author
are required fields.
POST api/books
{
"title": "The Anatomy of Melancholy",
"author": "Robert Burton",
"publication_year": 1621
}
response
201 Created
{
"pk": 6,
"title": "The Anatomy of Melancholy",
"author": "Robert Burton",
"publication_year": 1621,
"featured": false,
"reviews": []
}
By today or tomorrow, your app should respond with json to GET requests for all habits/books, for one habit/book, and for one book/habitβs associated objects (daily records for habits; reviews or trackers for books). By tomorrow you should also have at least some of your POSTs working, and can begin working on updates and deletes.
π Make sure you test your endpoints in Insomnia to confirm that they are working as intended.
Is your app deployed to Heroku yet? π π
π Resources
Serializers
- Using Different Read and Write Serializers in DRF
- How to Save Extra Data to a DRF Serializer
- Effectively using DRF Serializers
Permissions
- DRF Permissions
- Built-in Permission Classes in DRF
- Custom Permissions in DRF
- Pro Tip about DRF Permissions This shows how to combine permissions with logical operators like
and
andor