Coderbook

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

How to Squash and Merge Django Migrations

Django Migrations are one of the main features that keep me coming back to Django for most of the projects I work on. Being able to simply define the database model in python, and then sync it with the database schema using migrations adds so much value to a project.

Unlike frameworks such as PHP’s Laravel where you have to define your own migrations, Django will automatically create a migration file as soon as we change our model. It’s... Read more

Deep Dive Guide Into Python Type Hints and Type Checks

In 2012 a Finnish Ph.D. student from Cambridge named Jukka Lehtosalo approached the author of Python Guido van Rossum at a PyCon event regarding a new programming language named Alore that he had created which was heavily inspired by Python, but with the addition that it also supported optional type annotations.

Alore allowed you to write code with types, and then it used a transpiler to turn the code into pure Python. This pattern is popular and we can... Read more