Feb. 6, 2020
In Django, fields with auto_now=True are not always updated correctly. A common practice to work around this is to override the save() method on the model, or to use a pre-save hook. However, this doesn't work when calling Model.objects.update() since save() is not called on all instances in the queryset.
A common way to fix this would be to alter the table, for example, in MySQL it may look li...
Dec. 10, 2018
Instead of rolling my own progress bars on new batch management commands, I've been using tqdm lately. Here's a quick snippet for multiprocessing a batch of items in a queryset.
You'll need tqdm to run this: pip install tqdm
Note: Since the function that I'm calling is using django's ORM, I close all db connections. Things tend to go wrong when forked processes are all using the same conne...
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
inventory = models.IntegerField()
items = models.ManyToMany(Product)
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:
help = 'Create Shipping labels for upcoming shipments'
def handle(self, *args, **options):
start = datet...
Feb. 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...
Aug. 13, 2016
As your Django app grows larger, your test suite starts piling up. In a project I was working on we had a test suite of 375 tests. Today I'll explain how I reduced the run time of that suite from 350+ seconds to 20 seconds.
1. Mock, mock, mock!
A HUGE bottleneck of our tests was in the billing logic. We had many tests that were actually hitting billing APIs (on test accounts of course, but s...
June 30, 2016
Before turning to a django package for a new feature, it's sometimes a good idea to take a step back and look at how difficult it would actually be to do it yourself. Sometimes its easier to implement yourself rather than install a package for something simple. I noticed this with gravatar.
Gravatar is a service for globally recognized avatars. Many times this is used in sites so that we don't ...
March 2, 2016
Recently I have been playing around with decorators in Python. Decorators are a really cool feature that allows you to add functionality to any function with a simple @decorator tag. It is very nice for injecting functionality to many functions that do similar things.
A place where this can be applied effectively is in Django views. This past weekend I was at a hackathon working with a fairly s...
Feb. 2, 2016
While working on a project the other day I was presented with an interesting issue. In our project we are using Knockout JS for 2-way data binding between a set of data and a Leaflet JS map. With all of this data, we have many different click events needed so the user can interact with and manipulate the data in different ways.
I needed a way to have 2 different bindings on one item for both a ...
April 13, 2015
I designed drf-generators to allow you (the developer) to spend less time worrying about views and serializers and more time creating rock-solid data models for your application.
If you know a bit about Django, you can create a basic API in minutes. This tutorial will walk you through creating a complete API using the Django rest framework and drf-generators to jump-start your development.