RSSAmplifier

Blog

Johnny Metz

Recent content on Johnny Metz

johnnymetz.comRSS feed ↗28 posts

Latest posts

Django Custom Managers Are Silently Leaking Data

Django custom managers are a common way to exclude rows by default, such as inactive or soft-deleted rows. However, they don’t run everywhere you’d expect, which leads to unintended data exposure. This post covers where that happens and how to fix it. The Setup Let’s model stores and products with a soft-deletable relationship: class Store (models . Model): name = models .…

Django Time-Based Lookups: A Performance Trap

Django’s field lookups are one of the ORM’s best features, but time-based lookups can quietly bypass database indexes, turning fast queries into expensive full table scans. A Slow Production Query I ran into this while debugging a 30 second query on a large table (~25 million rows): class Event (models . Model): timestamp = models . DateTimeField(db_index = True ) Event . objects .…

Avoiding Duplicate Objects in Django Querysets

When filtering Django querysets across relationships, you can easily end up with duplicate objects in your results. This is a common gotcha that happens with both one-to-many (1:N) and many-to-many (N:N) relationships. Let’s explore why this happens and the best way to avoid it. The Problem When you filter a queryset by traversing a relationship, Django performs a SQL JOIN. If a parent…

7 Heroku Features Every Developer Should Be Using

Heroku makes hosting simple, but most developers overlook its advanced capabilities. After years of running apps on the platform, I’ve found several underrated features that speed up your workflow and make your apps more reliable. Here are seven you shouldn’t miss. 1. Preboot Preboot spins up new dynos and starts routing traffic to them before the old ones are terminated. The result is…

The Django db_default Gotcha That Breaks Your Code

Starting in Django 5.0, you can use db_default to define database-level default values for model fields. It’s a great feature but comes with a subtle and dangerous gotcha that can silently break your code before the object is ever saved. The Gotcha in Action Let’s say you’re building a simple task manager, and you want to track whether a task is completed: from django.db import…

Stop Using Django's squashmigrations: There's a Better Way

Squashing merges multiple database migrations into a single consolidated file to speed up database setup and tidy up history. Django’s squashmigrations command promises to handle this, but it’s error-prone and unnecessarily complex. A clean reset is faster and simpler. ⚠️ Why squashmigrations Falls Short Broken in Practice In medium-to-large projects, I’ve consistently seen…

Speed Up Django Queries with values() over only()

If your Django queries feel slow, the problem might not be your database — it might be your ORM. Recently, I was working with a query that took 25 seconds to run through the Django ORM, but the underlying SQL completed in just 2 seconds. With a single change, I got the ORM query down to 2 seconds as well — and reduced the memory footprint by 70%. Let’s walk through what happened, and why…

Avoiding PostgreSQL Pitfalls: The Hidden Cost of Failing Inserts

A simple insert query turned into a silent performance killer. Our frontend pings our server every few minutes to track device activity. Each ping attempts to insert a row into a DevicePingDaily table, which has a unique constraint on (device_id, date) to ensure only one record per device per day. In Django, the logic looked like this: try : DevicePingDaily . objects . create(device = device, date…

Disable Redundant Gunicorn Access Logs on Heroku

When hosting an application on Heroku , managing logs efficiently is crucial for maintaining system health and keeping costs down. Heroku provides built-in logging for all incoming requests, but by default, Gunicorn , the Python HTTP server often used in Heroku deployments, also logs incoming requests. This duplication can clutter your logs, making them harder to parse and more expensive to store.…

5 Ways to Get the Latest Book Per Author in Django

In a Django application, fetching the latest record for each group is a common yet challenging task, especially when working with large datasets. Whether you’re building an analytics dashboard or managing grouped data, finding an efficient solution is key. In this blog post, we’ll explore five different approaches to tackle this problem, ranked from least to most effective based on…

Zero Downtime Django Deployments with Multistep Database Changes

Preventing downtime during deployments is crucial for maintaining service availability and ensuring a positive user experience. Blue-green deployments have emerged as a popular strategy to achieve this goal. However, they introduce challenges, especially when dealing with database changes. This article delves into what blue-green deployments are, why database changes can be tricky in this context,…

Mastering Code Search with JetBrains Scope

Background As software engineers, one of the most crucial skills we develop is the ability to search through code efficiently. Whether it’s finding a specific function, understanding how a certain feature is implemented, or tracing a bug, being able to quickly navigate a codebase is essential for productivity. However, many codebases can be complex and sprawling, leading to noisy search…

Supercharge Your Django App: 7 Sneaky Tricks to Crush Slow Database Queries

Optimizing Django query performance is critical for building performant web applications. Django provides many tools and methods for optimizing database queries in its Database access optimization documentation . In this blog post, we will explore a collection of additional and essential tips I’ve compiled over the years to help you pinpoint and resolve your inefficient Django queries. Kill…

Optimize Django Query Performance by combining Select Related and Prefetch Related

Introduction When building a Django application, one of the key challenges developers face is optimizing database query performance. Django provides two tools, select_related and prefetch_related , that reduce the number of database queries, and increase the performance of your application. This blog post explores the power of these two methods and how to combine them to maximize your…

Free up disk space with Docker prune

Docker is a platform for developing, shipping and running applications in isolated, lightweight and portable containers. It is a critical part of a developer’s toolbelt and one I use just about everyday. Docker can consume a large amount of disk space. Per the Docker documentation : Docker takes a conservative approach to cleaning up unused objects (often referred to as “garbage…

[Cross-Post] Django performance: Sort by a Custom Order

See the post here: Sort a Django Queryset by a Custom Order

Disable a direct push to GitHub main branch

On a development team, you never want to push directly to the main branch. Instead, you want to require changes to be made through pull requests so they can be properly reviewed by other developers. Some developers, including myself, occasionally forget to push to a new branch so I like to have an automated check to prevent this mistake. Here are two methods to block direct pushes to the GitHub…

Using Hadolint, a Dockerfile linter, To Enforce Best Practices

Misconfigured Docker containers can be slow, heavy and difficult to maintain. This leads to sluggish releases, high storage costs and frustrated developers. Moreover, inadequate Docker containers can be a major security liability. A recent report by Aqua Security found that 50% of new Docker instances are attacked within 56 minutes of being deployed. Docker problems can be devastating for any…

Dockerize a Next.js app using multi-stage builds

See the source code for a working example. Next.js is a popular React Framework that comes with a lot of great features such as hybrid static & server rendering, smart bundling, file system routing, and much more. I prefer it over vanilla React. When we dockerize a Next.js application, we want the final production and development images to be small, fast to build and easy to maintain. Let’s…

Dockerize your Cypress tests

See the source code for a working example. UI testing is a critical part of any modern web application. My favorite testing framework is Cypress which enables you to write clean, fast and reliable tests. It consists of two main commands: cypress run : Runs Cypress tests from the CLI without a GUI. Used mostly in CI/CD. cypress open : Opens Cypress in an interactive GUI. Used for local development.…

Free up Django CPU usage in Docker with Watchman

You can use watchman in your Django project to make auto-reloads more CPU efficient. Adam Johnson’s Efficient Reloading in Django’s Runserver With Watchman blog post does an excellent job describing how to set this up. I highly recommend giving it a read. The tutorial explains how to install watchman on macOS with brew install watchman but does not explain how to install it in a Python…

Set Django Model Field Choices in your Frontend the Right Way

In Django, you can define a set of choices for any field. If you’re using a SPA frontend, such as React or Vue, then you’ll need to access these choices in a form. Let’s look at two ways to do this. As an example, we’ll use the following Device model: class Device (models . Model): class Size (models . TextChoices): SMALL = 'S' MEDIUM = 'M' LARGE = 'L' name = models .…

5 ways to get all Django objects with a related object

In Django, a related object is a model instance used in the one-to-many or many-to-many context. For example, let’s look at the built-in User model, which has a many-to-many relationship with the Group model. class User (models . Model): groups = models . ManyToManyField(Group, related_name = 'groups' ) For any given User object, all linked Group objects are called “related…

Why you need to use Subqueries in Django

The Django ORM is a powerful tool but certain aspects of it are counterintuitive, such as the SQL order of execution. Let’s look at an example of this trap and how we can fix it using subqueries: class Book (models . Model): class Meta : constraints = [ models . UniqueConstraint( fields = [ 'name' , 'edition' ], name = ' %(app_label)s _ %(class)s _unique_name_edition' , ) ] name = models .…

Check your Django Migrations on every commit

Keeping your models in sync with your migrations is an important part of any Django app. My team and I frequently make changes to our models and we occassionally forget to create new migrations for those changes. This results in errors and data loss. Let’s look at an easy way to ensure your models and migrations are always in sync: We’ll use a simple Product model. class Product…

Find all N+1 violations in your Django app

The N+1 problem is a common database performance issue. It plagues ORM’s, such as Django and SQLAlchemy, because it leads to your application making more database queries than necessary. Let’s look at a basic example in Django. class Artist (models . Model): name = models . CharField(max_length = 255 ) class Song (models . Model): artist = models . ForeignKey(Artist, on_delete = models…

(untitled)

About Me Full Stack Software Engineer located in San Francisco, CA. I specialize in web development with Python, Django, React, Docker, Kubernetes and AWS. Popular YouTube Videos Side Projects Notifire

Subscribe

Subscribe to my blog