Coderbook

Prevent Terraform from Recreating or Deleting Resource

Believe it or not, during the past few months I’ve managed to delete the database of this website not just once but twice (almost three!). Imagine that, how can someone be so clumsy to delete their whole database?!

Well, at least I’ve learned my lesson. Obviously, it wasn’t that I just wrote DROP DATABASE or anything like it by accident, what actually happened was that I accidentally reprovisioned my database instance which recreated a fresh version with a... Read more

Review of Creating Process Flows using Django Viewflow

During the last few months, I’ve been working on a project for one of the largest retailers in the world where we use machine learning and data science to help them predict future sales.

This global company has a huge amount of data and one of the trickiest parts of the project is to gather all the data from their stores, users, and markets. Some data is accessible from API’s while other data must be manually uploaded in Excel or... Read more

Add New Non-Null Foreign Key to Existing Django Model

Did you ever attempt to add a new non-nullable ForeignKey field to an existing Django model? If you did, you were probably prompted to add a default value that existing data should use for the new Foreign Key. But what value should this be? How do you automatically set it to the right value?

foreign key default value prompt in terminal

I was recently working on a project where the requirements had greatly changed throughout the iteration... Read more

How to Unit Test Functions Without Return Statements in Python

Unit Testing is one of the core concepts within programming that any software engineer should have a proper understanding of. Normally, we usually test and assert that a method returns a certain response, but what about functions that don’t return any response? How do we test and assert that they do what we intend them to do?

By using mocks, we can assert that given objects behave in expected ways. We can assert many different things such as:

How Scalable are Websites Built in Django Framework?

Scalability is one of the key concerns that you should take into account when you are planning to build a new application. Your application might start off small, but with time it might grow larger and you definitely want to avoid any kind of rewrite. Because of this, it is important to pick a framework that is easy to get going with, but that is also easy to scale.

So what does “scaling” mean in the context of web applications?... Read more

Deep Dive into Building a REST API The Correct Way

RESTful API’s are one of the main design patterns that you choose between when you’re setting up a new API for your application. The term gets thrown around a lot, and unfortunately many API’s ends up becoming some kind of mix of multiple patterns that are half-way implemented due to lack of understanding on what REST actually is.

This creates an API that feels unnatural for developers to use which might slow down development and confuse programmers. Usually, the reason... Read more

Deep Dive into Python Mixins and Multiple Inheritance

In my opinion, Mixins is one of the most powerful, and useful features introduced in object-oriented python programming. It allows you to compose your classes in unique and reusable ways that simplify your code and in turn also helps you avoid repeating yourself when programming.

So what is Mixins? If you come from another language you might already have seen inheritance or even multiple inheritance, but Mixins are probably a new term for you if you’re reading this article. What... Read more

Generate Thumbnails Automatically in Django with sorl-thumbnail

Generating thumbnails for your media content that is in the correct size, and optimized for the web by stripping any additional metadata away from them, is one of the main things that you will be recommended to do if you want to improve your websites load speed.

This can, however, be a quite tedious job, it might require you to install special software on your computer, and it might duplicate the work needed for adding media content to your posts.... Read more

Django's Class Based Views vs Function Based Views

As a Web Framework, one of the main uses for Django is to serve HTTP Responses back to the client that is visiting the website. This is done through what Django calls “Views”. A View is simply just a callable that returns a HTTPResponse.

For any beginner who starts reading through the Django Documentation, they will quickly be introduced to what we call Function-Based Views (FBV), this is just a simple function that returns a response.

For... Read more

How to Change Name of Django Application

Updating the name of an existing Django App is easier said than done. After you’ve renamed your folder name you will instantly be met with errors and different issues that result from dependencies to the old app name.

Many times the first version of your application is not the final one, and your code base might go through thousands of iterations where you slowly add, remove or change the features of your app.

Sometimes these changes are major refactors which... Read more