Momentum logo
Team 14 Classroom

๐Ÿป Views and Serializers in DRF ๐Ÿป

Posted on Sep 15th, 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 to document this list of endpoints 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 data to GET requests for all habits(or books), for a single habit(or book), and for one habitโ€™s (or bookโ€™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 UPDATE and DELETE.

๐Ÿ’ Make sure you test your endpoints in Insomnia to confirm that they are returning what they should, and NOT returning what they shouldnโ€™t. Use show_urls from django-extensions to help you check what urls you have defined.

Is your app deployed to Heroku yet? ๐Ÿ‘€ ๐Ÿš€

๐Ÿ”– Resources

Serializers

Permissions

๐Ÿ‘พ Code

Back to home