django-planet

Blog: Adam Johnson

web https://adamj.eu/
Author Adam Johnson

Django: Introducing Djade, a template formatter

Sept. 25, 2024 » Adam Johnson » [Archived Version]

Happy DjangoCon US 2024 to you. Whilst I am not there, I have adopted the spirit of the season and got to work hacking together a new tool. Djade is a formatter for Django templates. It applies rules based on the template style guide in Django’s contribution documentation, plus …

Read More

Django: speed up tests slightly by disabling update_last_login

Sept. 17, 2024 » Adam Johnson » [Archived Version]

Django’s test client provides two methods to log in a user: login() and force_login(). The latter one is faster because it bypasses the authentication backend, including password hashing, and just sets a session to have the user logged in. Typically, you’d want to use it in setUp() like …

Read More

Django: hoist repeated decorator definitions

Sept. 7, 2024 » Adam Johnson » [Archived Version]

Django provides us with a rich set of view decorators. In this post, we’ll look at a technique for hoisting repeated use of these decorators to reduce repetition. Repeated @cache_control calls Here are two public views with the same @cache_control decorator: from django.views.decorators.cache import cache_control @cache_control …

Read More

Django: a pattern for settings-configured API clients

Sept. 4, 2024 » Adam Johnson » [Archived Version]

Here’s an example of a common pattern in Django projects: from acme.api import APIClient from django.conf import settings acme_client = APIClient(api_key=settings.ACME_API_KEY) def order_anvil() -> None: acme_client.anvils.order(...) An API client is instantiated as a module-level variable based on some settings. This approach has some drawbacks …

Read More

Django: build a Microsoft Teams bot

Sept. 3, 2024 » Adam Johnson » [Archived Version]

Recently, I built a Microsoft Teams bot for a client, inside their Django project. It wasn’t fun or easy, but the experience did increase my resiliency as a developer. I also went into this forewarned by my wife, a product manager also known as “the integration queen”, who has …

Read More

Django: avoid “useless use of .all()”

Aug. 30, 2024 » Adam Johnson » [Archived Version]

Here’s a little ORM pet peeve of mine that may deepen your understanding of how QuerySets work. Take this code: Digger.objects.all().filter(height_cm__gt=200) The .all() is unnecessary. It’s equivalent to write: Digger.objects.filter(height_cm__gt=200) Why? The manager, Digger.objects, already refers to …

Read More

Django: rotate your secret key, fast or slow

Aug. 30, 2024 » Adam Johnson » [Archived Version]

Django’s SECRET_KEY setting is used for cryptographic signing in various places, such as for session storage and password reset tokens. This makes keeping it secure a high priority since an attacker with the key could forge things like password reset tokens. If you have leaked your secret key, you …

Read More

Django: create sub-commands within a management command

Aug. 13, 2024 » Adam Johnson » [Archived Version]

argparse, the standard library module that Django uses for parsing command line options, supports sub-commands. These are pretty neat for providing an expansive API without hundreds of individual commands. Here’s an example of using sub-commands in a Django management command: from django.core.management.base import BaseCommand class Command …

Read More

Django: Test for pending migrations

June 22, 2024 » Adam Johnson » [Archived Version]

This post is an adapted extract from my book Boost Your Django DX, available now. Django requires every change to model fields and meta classes to be reflected in database migrations. This applies even to things that don’t typically affect the database, such as Field.choices. When iterating on …

Read More

Django: Introducing django-harlequin, a launcher for Terminal-based SQL IDE Harlequin

May 6, 2024 » Adam Johnson » [Archived Version]

Harlequin is a Terminal-based SQL IDE by Ted Conbeer. It is pretty popular, with over 2,500 GitHub Stars and counting. It looks like this: Harlequin is built with Textual to give a very interactive experience on the terminal. You can click around its UI and use keyboard shortcuts. I …

Read More