Django Queryset Annotations for Increased Performance

June 5, 2017

Django querysets are really powerful. They can often be used to reduce expensive object iterations down to the database level. Let's take the following models and figure out which product has the least in stock. from django.db import models class Product(models.Model): inventory = models.IntegerField() class Package(models.Model): items = models.ManyToMany(Product) class Or...

Mutiprocessing in Python Django Management Commands

May 19, 2017

A problem that many applications (hopefully) eventually run into is scalability. In a project I'm working on, we have a django management command that makes a time consuming API call to update a model. Let's take a look at the original code: class Command(BaseCommand): help = 'Create Shipping labels for upcoming shipments' def handle(self, *args, **options): start = datet...

Using Template Variables to control Django Themes

February 13, 2017

A problem I recently ran into was allowing for multiple themes to be used on the same project. I decided to set a django setting called BRAND in order to control the theme through separate css files. For this example, we will assume that your static directory has sub-directories for each brand, with a file site.css in each. Now, in order to inject our brand setting into the context, we need...