December 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...
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...
August 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...
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.
March 29, 2015
Today I was working to continue to improve my website and came across some really helpful stuff to improve the Django admin. By default, the django admin has a really simple interface for managing objects in your database. For example, the page for managing my Pages looks like this
This works and is much nicer than having to build my own admin, but there is a lot more power hidden in Django...
March 22, 2015
I have recently moved away from PHP and begun working with more all-purpose languages, Python being the main one. While experimenting with this new language, I rewrote my blog to use the Python web framework Django. Django 1.7 with Python 3.x is not supported by default on Dreamhost's shared servers, so here is how I installed my own custom version of Python to deploy my app.
In this tutorial I...