🐻 Designing Your Models & Your API 🐻
Posted on Sep 20th, 2022
🗓️ Today’s Topics
- Models for your collaborative project
- Designing the URLs you need
- CORS
🎯 Project
Today you should have your models nailed down and be able to work with them in the admin and the shell – that is, you can save them to the database and retrieve them, and all the relationships work properly.
You should have a plan written down for at least the basic set of urls that your front-end will need. You may make changes to this as you go, but you need a place to begin.
When creating your list of endpoints, remember to only build what you need.
- What lists or collections of data will you need to return? (There are probably some you can omit.)
- What single resources will you need to provide?
- What HTTP methods will you need to support for each endpoint? (There are probably some you don’t need.)
- Which routes will need the user to be authenticated and which do not require authentication?
- Will you need to restrict any actions at the object level? For example, you’ll probably want to make sure that only the owner of a resource can make changes to it, or add associated objects.
By Friday
Your API should…
- return JSON for GET requests for questions and answers OR cards and friends
- accept POST requests to create questions and answers OR cards and friends
🔖 Resources
- A Long (Mostly Helpful) List of Things to Keep In Mind When You’re Building an API -> Many of these best practices you get for free with DRF; some are included with packages we use; and some of this is advice for the choices you have to make in the code you write.
- Overview of creating an API -> Nothing much new in this article, but it is a good summary and high-level overview of how to go about creating an API with DRF.
- How to Save Extra Data to a DRF Serializer -> This is essential for adding in extra info that is not included in your serializer, like the user (from request.user) that is associated with the object you want to create.
Permissions
- DRF Permissions
- Built-in Permission Classes in DRF
- Custom Permissions in DRF
- Pro-Tip: Logical operators with DRF Permissions
CORS
- MDN CORS
- Julia Evans comic explaining CORS better than MDN does
- Another great visual explanation of CORS
- Django CORS Guide
django-cors-headers
⭐ TMI Databases: Deeper Dive into the Theory Behind the Best Practices
This is very much optional right now but important to learn about at some point if you’re going to be working with data in your job.
- An Introduction to Database Normalization
- Video on the first normal form
- Video on the second normal form
- Video on the third normal form