How to Optimize Your Django REST Viewsets

The combination of Django and the Django REST framework is powerful. With just three classes (a model, a viewset, and a serializer), you can have a basic CRUD endpoint up and running. Although it is easy to get set up, it is also easy to end up with a view that makes hundreds of unnecessary database queries. As database queries are relatively slow, we want to avoid them as much as possible. In order to do this, we will follow Tip #4 from my Django ORM Optimization Tips post:

4. Use select_related() and prefetch_related() when you will need foreign-key/reverse related objects.

Continue reading How to Optimize Your Django REST Viewsets

Write Better Functions

Functions are the building blocks of many applications. Most software developers, current or aspiring, have some experience writing functions. Everyone writes them differently, and there are different approaches to getting the most out of your functions. Bad functions are hard to maintain, more unstable, and lack reusability. Writing better functions will make your application more stable, easier to read and maintain, and make future development easier.

Continue reading Write Better Functions

Django Function-Based Views vs. Class-Based Views

Django views are an essential part of most Django applications. They carry out user requests to perform actions and return different types of data. Django provides two distinct methods for creating a view: function-based and class-based. Many discussions have been had over the benefits of using one over the other. I believe that the benefits of using one or the other is highly circumstantial. In this post, I will outline the guidelines I use to determine which option is better for different circumstances.

Continue reading Django Function-Based Views vs. Class-Based Views