Django

Building a Production Ready Django JSON API

Outline

This course is our answer to the pleas of would-be Djangonauts for production-ready products. Our hope is that we have set a path for you to follow and that we have left nuggets of wisdom for you to internalize along the way. I can’t promise this will be easy — nothing worth having ever is — but, if you’re willing to put in the work, then we are confident this course will be an early checkpoint on your road to success.

About the Author

Let me introduce myself.

My name is Derek Howard. I've spent my career working with several different companies in many different industries working to create and manage web solutions that improve efficiency and performance.

I've always loved the process for taking an idea and building up the technology solution that brings it to realization. From the fortune 500 investment world to online medical libraries - wholesale distribution in the diesel performance industry - consulting for state agencies - large energy companies to chatbot solutions using NLU and Machine Learning; I've done projects on my own, worked with and lead small teams, medium multi-level experienced teams, and large distributed teams coordinating with development partners in India and Russia.

Recently, I was working to bring together teams from 3 separate companies that were acquired and charged with building out several medium sized distributed teams to work on multiple software products simultaneously to build out customer service chatbots using AI, Machine Learning, and Natural Language Understanding. We used Python and Django as the platform of choice for our real world solutions.

Goals Of This Course

Our goal for this course is to bridge the gap between toy projects and production-ready products. To accomplish this, we have designed an API that will power an MVP-level product. This course will walk you through building that API yourself.

We do not concern ourselves with questions like “Will it scale?”. These questions are, for now, unimportant and only serve to slow you down. Instead, we’ve focused on crafting something that will get the job done while following a single principle: make good decisions. We actively avoid things we know to be bad and we frequently make trade-offs to balance getting it done and getting it done right. Done is better than perfect.

Note: If you’re interested in a more advanced course covering topics like scaling, deployment, etc, check out our other courses on Thinkster.

Final Product

The API we are going to build together will support the following features:

Authentication

What good is a web app that users can’t log into? We will handle registration and login in this chapter. We’re also going to replace Django’s default session-based authentication with token-based authentication using JSON Web Tokens. We will introduce the concept of authorization (or permissioning) and make sure that only the logged in user can edit their own information.

This will be the longest chapter because there are so many concepts to be introduced.

Profiles

After all the security stuff is handled, we will need a way to store information about each user that isn’t related to authentication. Things like a biography, the user’s avatar, etc.

To do this, we will introduce user profiles and start experimenting with Django’s relationships by creating a one-to-one relationship between the user and profile classes. This means every user will have exactly one Profile object that gets created automatically when they register their account.

Articles

Our fancy new web app wouldn’t be much fun if all you could do was sign up and log in. To show that we’re fun people, we’re going to introduce the concept of an article. Articles can be posted by any user for other users to read, favorite, and comment on. Think of it like a blog where anyone can post new content.

Comments

“Going viral” is all the buzz these days. To make our site viral-friendly, we’re going to create a way for users to comment on the articles we just built. It’ll be just like Reddit!

Following

Continuing our discussion of creating relationships, users should be able to follow one another. We’re going to introduce the concept of unidirectional, many-to-many relationships. Think of these relationships in terms of Twitter vs. Facebook. On Facebook, if we’re friends, then that means I am your friend and you are my friend. Contrast this with Twitter. If you follow me, I could be following you, but I don’t have to be. We will take the Twitter approach because it makes more sense for our app.

Favoriting

Being able to favorite Articles is good for two reasons. First off, it lets you stroke the author’s ego. Secondly, it’s like your own personal bookmarking system. When a user favorites an article, we will create a relationship between the two. Then, if that user is looking for an article they read a while back that they enjoyed, they can just look at their list of favorited articles instead of scrolling back through their feed.

Tagging

One of the more difficult questions to answer when building a content platform like the one we’re building is “How do users find content that is interesting to them?”. That’s a toughy because there are so many options. On one end of the spectrum you have machine learning which can figure out what each user is interested in and can recommend content based on that knowledge of the user. On the other end, you’ve got a simple tagging system that asks the users to do the heavy lifting by assigning tags to the articles they publish.

Since we’re just looking to build an MVP, we’re going to use the latter approach. We will leave the machine learning for after we raise our C funding round and can throw tens of millions of dollars at the problem!

Pagination

As our site grows and becomes more popular, our library of content will grow enormously. Loading and rendering 10,000 articles with a single API request is a bad idea for performance reasons. To get around this, we’re going to have to break up those 10,000 articles into multiple smaller requests. This is where pagination comes into play.

Pagination lets us retrieve 20 articles at a time instead of retrieving all of the articles in the database at once. Django REST Framework gives us some tools to make this simple.

Filtering

We will also support filtering articles on a few different dimensions: by author, by tag, and by the user who favorited the article. This will give us another opportunity to help users find articles that will interest them. Maybe you’re starting to see a trend here. The goal of the API is to support the front end in giving users a pleasurable experience.

Django and Django REST Framework Primer

We do assume some prior experience with both Django and Django REST Framework. We will be covering a lot of ground in this course and having to explain every new concept from the ground up would do more harm than good.

Django's introductory tutorial covers the basics of Django and will give you sufficient understanding to complete this course.

If you’re interested in diving deeper into what Django has to offer, the API reference and the topical guides are amazing resources. You should get familiar with both if you plan to use Django for a production-ready application.

Check out the Django API Reference
Check out the Django Topical Guides.

Django REST Framework also has it's own tutorial that covers most of what the library has to offer.

Let’s get started

That’s all you need to know for now. To get started, clone the conduit-django repository and follow the instructions in the file called README.md to get everything installed and running.

I've cloned the repository and I'm ready to go. Let's do this.
 

I finished! On to the next chapter