🐻 Designing Your Models & Your API 🐻
Posted on Jun 1st, 2022
🗓️ Today’s Topics
- Models for your collaborative project
- Designing the urls you need
- Review permissions & 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 WRITTEN DOWN plan 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: only build what you need.
- What lists or collections of data will you need to return?
- What single resources will you need to return?
- What HTTP methods will you need to support for your urls?
- Which routes will need to be authenticated and which do not require authentication?
- Will you need to restrict any actions at the object level?
By Friday
Your API should…
- be returning JSON for GET requests for questions and answers / cards and friends
- be able to accept POST requests to create questions and answers / 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
Databases: Deeper Dive into the Theory Behind the Best Practices
- An Introduction to Database Normalization
- Video on the first normal form
- Video on the second normal form
- Video on the third normal form